Ok after testing checking my program seems to work fine, it reads in the command line ok, it checks all the files, it reads in the lines from both files and compares them for a difference, were if they are different it writes it to a third txtfile, All this works fine, it writes the relevent info to the file.
The problem i am having is that when i try to display the contents of the 3rd txt file it won't work at all.
I checked and dbl checked and i am 98% sure that its correct, can anyone see anything wrong or am i missing something.
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_LEN 256 /* max length of buffer string */
#define BUFFERSIZE 1000 /* 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[BUFFERSIZE];
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");
}
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 teh files
for errors and the files are still open.
-------------------------------------------------------------------------
*/
do
{
/* 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)
{
line++;
printf("File 1: Line %d :%s \n",line, buf1);
printf("File 2: Line %d :%s \n",line, buf2);
fprintf(fn3, "File 1 line:%d %sFile 2 line:%d %s\n",line, buf1, line, buf2 );
}
} while ( !feof(fn1) || !feof(fn2) );
/* 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 */
printf("Contents of the File '%s'\n",argv[3]);
while (! feof(fn3))
{
fgets(filebuff,BUFFERSIZE,fn3);
if(filebuff[0] == '\n');
{
printf("File is Empty or invalid\n");
break;
}
printf("\n%s\n",filebuff);
}
fclose(fn1);
fclose(fn2);
fclose(fn3);
return (0);
}