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);
}