Fscanf endless loop

This is a discussion on Fscanf endless loop within the C Programming forums, part of the General Programming Boards category; I'm getting a endless loop at the line I've marked. I'm making a program that compares two files and if ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    1

    Fscanf endless loop

    I'm getting a endless loop at the line I've marked. I'm making a program that compares two files and if there is something different on each line of the file, the program will report what line it is that has a difference between the two files. Anyways, I can get it working and debug the logic of the program myself, but before I can do that I need to figure out why I have a endless loop. I have it saying if it equals EOF, then to break out of the loop. But instead it just keeps repeating the same last letter in the file over and over. As you can see my other while loops have text2 != EOF. So I thought maybe text2 isn't stored the EOF, but only the value of fscanf outputs EOF, so thats why I the line I've marked is alittle different from the other while loops, because I was trying to test different things.


    Code:
    #include <stdio.h>
    
    int main(int argc, char *argv[]){
    
    
      int Counter1 = 1, k=0;
      char text1, text2;
      FILE *Fin, *Bin;
    
      Fin = fopen("testdata60","r");
      Bin = fopen("testdata60_2","r");
    
      while((fscanf(Fin,"&#37;c",&text1) != EOF) && (fscanf(Bin,"%c",&text2) != EOF)){
    
          if((text1 || text2) == '\n'){
            if(text1 == '\n'){
              if(text2 != '\n'){
                printf("\nThe files are not the same on line %d",Counter1);
                while( (fscanf(Bin,"%c",&text2) != '\n') || (text2 != EOF)) printf("%c",text2);
                printf("BOB");
              }
              Counter1 = Counter1 + 1;
            }
            else if(text2 == '\n'){
              if(text1 != '\n'){
                printf("\nThe files are not the same on line %d",Counter1);
                while( (fscanf(Fin,"%c",&text1) != '\n') || (text1 != EOF)) k=k+0;
                printf("BOB");
              }
              Counter1 = Counter1 + 1;
            }
          }
    
          else if(text1 != text2){
            printf("NEVER");
            printf("\nFiles are different on line %d",Counter1);
    
            while( (fscanf(Bin,"%c",&text2) != '\n') || (fscanf(Bin,"%c",&text2) != EOF)) printf("%c",text2); /* ENDLESS LOOP*/
            while( (fscanf(Fin,"%c",&text1) != '\n') || (text1 != EOF)) k=k+0;
          }
        }
    
      fclose(Fin);
      fclose(Bin);
    
      return(0);
    }
    Last edited by krum; 12-04-2007 at 06:59 PM.

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    nevermind
    Last edited by Dino; 12-04-2007 at 07:08 PM. Reason: premature conclusion

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,659
    IF you're just going to compare two text files (which I assume you are), then use fgets to read a line from both files, then compare them with strcmp. Much easier and less messy.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,588
    > if((text1 || text2) == '\n')
    This doesn't mean what you think it means.
    Try
    text1 == '\n' || text2 == '\n'
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. using data without fscanf again
    By jacey in forum C Programming
    Replies: 13
    Last Post: 11-03-2007, 03:44 AM
  2. Rewriting a for loop as a while/do-while loop
    By Ashfury in forum C++ Programming
    Replies: 7
    Last Post: 04-27-2007, 02:20 PM
  3. I need help as soon as possible.
    By hyrule in forum C++ Programming
    Replies: 7
    Last Post: 11-09-2005, 04:49 PM
  4. How to change recursive loop to non recursive loop
    By ooosawaddee3 in forum C Programming
    Replies: 1
    Last Post: 06-24-2002, 08:15 AM
  5. Endless loop!
    By Paninaro in forum C Programming
    Replies: 3
    Last Post: 06-23-2002, 08:15 PM

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