Hi all,
I’m kind of newbie in C programming. I am trying to write a little application which compares 2 text files from the user, compare between them (line by line) and writes in a third file the differences between the 2 first.
I have chosen to use “fgets” command to read the lines, but my program crashes. There are no compilation errors. It fails on the “While”
Can you please take a look?
Code:
#include <stdio.h>
#include <stdlib.h>
void main ()
{
FILE* firstHandle;
FILE* secondHandle;
char* firstFileLine=NULL;
char* secondFileLine=NULL;
char firstFile[15];
char secondFile[15];
//interface
printf ("\nPlease enter filename of first file: ");
scanf ("%s",firstFile);
printf ("\nPlease enter filename of second file: ");
scanf ("%s",secondFile);
// open both files
firstHandle=fopen (firstFile,"rt");
if (firstHandle==NULL)
{
printf ("\nCannot open firstfile: %s\n",firstFile);
exit(0);
}
secondHandle=fopen (secondFile,"rt");
if (secondHandle==NULL)
{
printf ("\nCannot open second file: %s\n",secondFile);
exit(0);
}
//let's read the lines until eof of the first given file.
while (fgets(firstFileLine, 50, firstHandle)!=NULL)
{
//Do the compare stuff here
}
}