Thread: Double While Loop Problem

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    2

    Double While Loop Problem

    Hey Guys,

    I am comparing two files and for every line that is the same, I am putting a star in front of it and putting it in a new file. However, for some reason it isn't working for me. Any ideas of what's wrong?

    Code:
            
    char one[2048];       
    char two[2048];
    
    while (fgets(two, 2048, fileB) != NULL){
        while (fgets(one, 2048, fileA) != NULL){
            if (strncmp (one, two, 2048) == 0){
                fprintf(fileC, "*%s", two);
            }
        }
    }
    Any help would be much appreciated! Thank you!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Yes, when you get to the end of file one, you have to use rewind() if you want to read it again.

    Though if one is more than a few lines long, it would be better to read all of one into an array in memory, and save yourself an awful lot of repetitive file operations.

    BTW, nice job on using code tags on your first post. Makes a change to see - keep it up.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    2
    Awesome thank you!!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to come out of double loop
    By suryak in forum C Programming
    Replies: 18
    Last Post: 07-06-2011, 01:20 PM
  2. Double Hashing Infinite Loop
    By jlangfo5 in forum C++ Programming
    Replies: 4
    Last Post: 03-06-2011, 07:48 PM
  3. Replies: 6
    Last Post: 11-04-2010, 04:33 PM
  4. fprintf in a double while loop
    By wyoui in forum C Programming
    Replies: 2
    Last Post: 06-01-2010, 12:31 PM