Thread: prog !feof probs

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    27

    prog !feof probs

    Hi me again i hope someone can help, as you may not know my program reads in lines of text from 2 text files, it compares them and if they are different they write these lines to the third file

    The program works fine as long as the files have the same number of lines in the.

    However if i try comparing say file1 has 3 lines and file2 has 6, the program will not break out of the while loop, and i don't know why

    the while statement i use is as follows
    while ( (!feof(fn1)) || (!feof(fn1)) )


    it keeps comparing the 3rd line till you get to the end of file 2 or the 7th line. why is this??? plz help

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    #define MAX_LEN 256 /* max length of buffer string */
     /* Used to display 3rd txt file*/
    
    /* Set up variables */
    int reperror, x, line;
    FILE *fn1, *fn2, *fn3;
    char buf1[MAX_LEN];
    char buf2[MAX_LEN];
    char filebuff[MAX_LEN];
    
    int main(int argc, char *argv[])
    {
    
      /*
      -------------------------------------------------------------------------
      Check to see that 3 command arguments were entered along with the program
      executable file, and test them to see if they will work.
      -------------------------------------------------------------------------
      */
    
      reperror = 0;
    	line = 0;
    
      if (argc == 4)
    	{
    	  printf(" You Entered a correct amount of information\n");
    	  
    		if((fn1 = fopen(argv[1] ,"r")) == NULL)
    		{
    		  printf("There was an error with File 1\n");
    			reperror++;
    		}
        else
    		  printf("File 1 Checked out okay\n");
    
    	  if((fn2 = fopen(argv[2] ,"r")) == NULL)
    		{
    			printf("There was an error with File 2\n");
    			reperror++;
    		}
    		else
    			printf("File 2 Checked out okay\n");
    		
    		if((fn3 = fopen(argv[3] ,"r+")) == NULL)
    		{
    			printf("There was an error with File 3");
    			reperror++;
    		}
    		else
    			printf("File 3 Checked out okay\n");
    
    
        /* If there has been any errors the program terminates*/
        if (reperror > 0)
    	{
    	  printf(" Sorry There was a problem with opening the files, please check/n ");
    	  printf(" Filenames using error messages above!!!\n ");
    	  printf(" program terminated\n ");
    	  exit(1);
    	}
        else
          printf("All the files check out okay, program will continue....\n\n");
    
    	}
    	else
    	{
    	  printf("There was and error, you did not enter enough\n");
    	  printf("Filenames from command prompt please check and\n");
    	  printf("correct\n");
    	  exit(1);
    	}
    
    /*
    -------------------------------------------------------------------------
    This is the actual program, the previous part of the program checked the
    files for errors and the files are still open for the next part of the
    program, this save open and closing them 
    
    The Next part reads in from both txt files a line at a time and compares 
    them, it then writes them to file 3 if they are different.
    
    The final part is displaying the the contents of txtfile3, this required
    rewinding the file to the start. 
    -------------------------------------------------------------------------
    */
    
      while ( (!feof(fn1)) || (!feof(fn2)) );
      {
        line++; /* Used to count lines*/
    		/* Gets a line of text from each txt file usally upto end of line char
    	   or reaches the buffer */
        fgets( buf1, MAX_LEN, fn1 );
        fgets( buf2, MAX_LEN, fn2 );
    
        x = strcmp(buf1, buf2);
        /* x = < 0 Str1 is less then str2 
           x = 0  Str1 is equal to str2
    	     x = > 0 Str is greater than str2
    	     if x not = 0 then display and write lines to text file 3 */
        
    	if ( x != 0)  
    	{
    	  printf("\nFile 1: Line %d :%s",line, buf1);
    	  printf("\nFile 2: Line %d :%s",line, buf2);
          fprintf(fn3,"File 1 line:%d %sFile 2 line:%d %s",line, buf1, line, buf2);
    	}
      
      }
     
    	/* While file 1 or file 2 is not fEOF keep reading in lines and comparing
         With the files check print whats in the text file 3
         if it empty it will say so else it will display 
         the files contents to the screen */
    	
    	rewind(fn3); /* Need to rewind file 3 to start so that it can be read for content */
      
    	printf("\nContents of the File '%s'\n",argv[3]);
    	
    	while (!feof(fn3))
    	{
    	  fgets(filebuff,MAX_LEN,fn3);
    	
    	  if (filebuff[0] == '\0') /* if the file is empty break */
    		{
    		  printf("The file is empty or there was an error\n");
    		  break;
    	  }
    		
    	  printf("%s",filebuff);
    	  
    	}
    	
    	fclose(fn1);
    	fclose(fn2);
    	fclose(fn3);
    	
    	return (0);
    }
    Last edited by @licomb; 08-23-2001 at 08:07 PM.

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    try changing ( (!feof(fn1)) || (!feof(fn1)) ) to ( (!feof(fn1)) && (!feof(fn1)) )
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    27
    I tried that it still does the same thing

    I ment to say
    while ( (!feof(fn1)) || (!feof(fn2)) ),

    but i have also tried this (!feof(fn1) && !feof(fn2)), it still doesn't work, and i can't seem to think why it should break out the loop once one file meets the eof.

    -ali
    Last edited by @licomb; 08-23-2001 at 08:09 PM.

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    27
    Ok i managed to get the program to work but it required me to take out the !feof, after a bit of digging i found out that this functions wasn't all that reliable so I did the following, hope this helps anyone who might do similar.

    while ( (fgets( buf1, MAX_LEN, fn1 ) != NULL) && (fgets(buf2, MAX_LEN, fn2 ) !=NULL) )



    -ali

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Assembly amature question: prog errors
    By geek@02 in forum Tech Board
    Replies: 1
    Last Post: 10-03-2008, 01:35 PM
  2. need help making a dot bounce up and down y axis in this prog
    By redwing26 in forum Game Programming
    Replies: 10
    Last Post: 08-05-2006, 12:48 PM
  3. an option at the end of this prog to ask if I want to run again
    By bandito9111 in forum C++ Programming
    Replies: 2
    Last Post: 03-31-2003, 02:30 PM
  4. feof ....EOF
    By GanglyLamb in forum C Programming
    Replies: 12
    Last Post: 12-04-2002, 12:38 PM
  5. advice on feof()
    By Hobo in forum C Programming
    Replies: 2
    Last Post: 09-07-2002, 08:15 AM