Thread: Fscanf endless loop

  1. #1
    Registered User
    Join Date
    Dec 2007

    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.

    #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);
              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;
              Counter1 = Counter1 + 1;
          else if(text1 != text2){
            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;
    Last edited by krum; 12-04-2007 at 07:59 PM.

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

  3. #3
    Join Date
    Oct 2007
    Inside my computer
    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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    > if((text1 || text2) == '\n')
    This doesn't mean what you think it means.
    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.

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, 04: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, 03:20 PM
  3. I need help as soon as possible.
    By hyrule in forum C++ Programming
    Replies: 7
    Last Post: 11-09-2005, 05: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, 09:15 AM
  5. Endless loop!
    By Paninaro in forum C Programming
    Replies: 3
    Last Post: 06-23-2002, 09:15 PM
Website Security Test