loop the loop - feeling foolish

This is a discussion on loop the loop - feeling foolish within the C Programming forums, part of the General Programming Boards category; 2 files - select each item in the second file and search for each matched item in the first file ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Location
    Bearn, France
    Posts
    2

    loop the loop - feeling foolish

    2 files - select each item in the second file and search for each matched item in the first file
    when an item is found - print something - select next item in second file and search first file again from the beginning --- but the inner loop continues from where it found the last match - tried using break and continue keywords - but to no avail
    the solution cannot be difficult but i'm having a dumb day

    thanks in advance for and hints

    file 1
    -------------
    pollock
    cod
    shark
    haddock
    minnow
    hake
    cod
    goldfish

    file 2
    ------------
    haddock
    cod

    Code:
       while(fgets(line2, sizeof line2, fin2)){
       printf("outer loop reads = %s", line2);
          while(fgets(line, sizeof line, fin)){
          printf("inner loop read = %s", line);
          countlines++;
             if(strcmp (line2, line) == 0){
                printf("\nFound %s on line %d\n", line2, countlines);
                printf("line2 = %s and line = %s\n", line2, line);
                break;
             }
          }
       }
    output
    ------------
    outer loop reads = haddock
    inner loop read = pollock
    inner loop read = cod
    inner loop read = shark
    inner loop read = haddock

    Found haddock
    on line 4
    line2 = haddock
    and line = haddock

    outer loop reads = cod
    inner loop read = minnow
    inner loop read = hake
    inner loop read = cod

    Found cod
    on line 7
    line2 = cod
    and line = cod

  2. #2
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    You have to set the position indicator of the "fin" stream back to the beginning. This is done with the fseek() command:

    Code:
    fseek ( fin , 0 , SEEK_SET );
    The same thing can be achieved with rewind():

    Code:
    rewind(fin);

  3. #3
    Registered User
    Join Date
    Apr 2007
    Location
    Bearn, France
    Posts
    2

    Thanks

    Thank you KONI - both solutions work perfectly

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 11:21 PM
  2. While loop misbehaving (or misunderstanding)
    By mattAU in forum C Programming
    Replies: 2
    Last Post: 08-28-2006, 03:14 AM
  3. loop in a linked linked lists
    By kris.c in forum C Programming
    Replies: 6
    Last Post: 08-26-2006, 01:38 PM
  4. while loop help
    By bliznags in forum C Programming
    Replies: 5
    Last Post: 03-20-2005, 12:30 AM
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 09:14 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21