Like Tree2Likes
  • 1 Post By Bryan Lassiter
  • 1 Post By Salem

Double While Loop Problem

This is a discussion on Double While Loop Problem within the C Programming forums, part of the General Programming Boards category; Hey Guys, I am comparing two files and for every line that is the same, I am putting a star ...

  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!
    Salem likes this.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,854
    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.
    rogster001 likes this.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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, 02: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, 05:33 PM
  4. fprintf in a double while loop
    By wyoui in forum C Programming
    Replies: 2
    Last Post: 06-01-2010, 01:31 PM

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