-
My program, anyhelp
I am writing a program that when run from the terminal in linux (redhat), it is to take in 3 filenames of txt files, the first 2 file are used to compare lines of text to see if they are different if they are then they are wrote to the 3rd file, the program is to then prinf the contents of the third file to the screen.
I have not been able to test the program as my other machine is down and i don't get it back till friday, so i can't test it, so i am having to write it by hand.
Can anyone help me to see if this will work or if there
/************************************************** **/
/* Author: Alisdair Comb */
/* Filename: checker.c */
/* Date: 27/07/01 */
/* Program: File Checker v1.0a */
/* */
/************************************************** **/
/* About the Program */
/* This Program is designed to take lines of text */
/* from 2 seperate files (txt) and compare the */
/* lines of text, if they are different, then */
/* the line of text will be wrote to a third */
/* file, after which it will display the contents */
/* of that file. */
/************************************************** **/
#include <stdio.h>
#include <string.h>
#define MAX_LEN 80;
int reperror, x;
FILE *fn1, *fn2, *fn3;
char buf1[MAX_LEN];
char buf2[MAX_LEN];
int main(int argc, char *argv[])
{
/*
-------------------------------------------------------------------------
Check to see that 3 command arguments were entered along with the program
executable file.
-------------------------------------------------------------------------
*/
reperror = 0;
if (argc == 3)
{
printf(" You Entered a correct amount of information");
if((fn1 = fopen(argv[1] ,"r")) = NULL)
{
printf("There was an error with File 1");
reperror++;
}
else
{
printf("File 1 Checked out okay");
}
if((fn1 = fopen(argv[2] ,"r")) = NULL)
{
printf("There was an error with File 2");
reperror++;
}
else
{
printf("File 2 Checked out okay");
}
if((fn1 = fopen(argv[2] ,"w")) = NULL)
{
printf("There was an error with File 3");
reperror++;
}
else
{
printf("File 3 Checked out okay");
}
/* 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!!! ");
prinft(" 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");
printf("Filenames from command prompt please check and");
printf("correct");
exit(1);
}
/*
-------------------------------------------------------------------------
-------------------------------------------------------------------------
*/
do
{
// Gets a line of text from each txt file usally upto end of line char
// or reaches the buffer
fgets( buf1, MAX_LEN, argv[1])
fgets( buf2, MAx_LEN, argv[2])
x = strcmp(buf1, buf2)
if ( x != 0)
{
printf("File 1: Line n :%s \n",buf1);
printf("File 2: Line n: %s \n",buf2);
fprintf(argv[3], "%s \n %s \n", buf1, buf2 );
}
} while ( argv[1] != EOF &| argv[2] != EOF)
// While file 1 and or file 2 is not EOF
// With the files check print whats in the text file 3
if ( argv[3] == Null)
printf("The File was empty!!!");
else
printf("%s" argv[3]); // Print contents of txtfile 3 to screen
return 0;
}
I am usure about the while loop, not sure if you can do a and or situation. Also stinking i am not sure if i need to empty buf1 and buf2, do i need to.
Thanks for any help I am most grateful.
-ali
-
I don't know if your program will wprk as intended but you'll want to change some of your conditional statements
if((fn1 = fopen(argv[1] ,"r")) = NULL)
to
if((fn1 = fopen(argv[1] ,"r")) == NULL)
-
I've just noticed i haven't closed all the files,will add that to end of program.
-
Thanks, just noticed that myself, also i have changed the code so that it use fn1, fn2 and fn3 probalym was still using argv[*], which would no doubt cause an error.
Sorry, a while since i programed and i am used to having a compiler handy.
-ali
-
Some more:
1) Your define statement is incorrect.
2) You've include a semi colon directly after an if statement.
3) They are some typos 'prinft'.
4) Header file not included for exit() function.
5) Incorrect checking for EOF.
6) Final printf() call missing comma.
There may be more.
-
I fixed the while statement to || from |&
Thanks, fixed all those errors, oh i miss my compiler, looking at the program will it do what i looks like it will do.
-
The file pointer won't tell you when the end of file has been reached. The result of fgets() will, and you're missing some semi-colons from the end of your function calls.
-
so does this mean i would to change the while statement to
do
{
} (buf1 != EOF || buf2 != EOF )
Not sure if iam getting what you are saying :(
-ali
-
........and you've written NULL as Null.
and I'm now a member:)
-
Thanks for the help Zen, its greatly appricated, good to know there are some helpful people out there willing to help.
Do you think the program will do what is supposed to do.
-ali
-
Sorry if you want to test for end of file using file pointer you can use the feof() function. So you want -
while (!feof(fn1)) etc.
-
I would never of thought to use that :( thanks
-
Here's teh code with the errors fixed (I hope)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_LEN 80
int reperror, x;
FILE *fn1, *fn2, *fn3;
char buf1[MAX_LEN];
char buf2[MAX_LEN];
int main(int argc, char *argv[])
{
/*
-------------------------------------------------------------------------
Check to see that 3 command arguments were entered along with the program
executable file.
-------------------------------------------------------------------------
*/
reperror = 0;
if (argc == 3)
{
printf(" You Entered a correct amount of information");
if((fn1 = fopen(argv[1] ,"r")) == NULL)
{
printf("There was an error with File 1");
reperror++;
}
else
{
printf("File 1 Checked out okay");
}
if((fn2 = fopen(argv[2] ,"r")) == NULL)
{
printf("There was an error with File 2");
reperror++;
}
else
printf("File 2 Checked out okay");
if((fn3 = fopen(argv[2] ,"r+")) == NULL)
{
printf("There was an error with File 3");
reperror++;
}
else
printf("File 3 Checked out okay");
/* 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!!! ");
prinft(" 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)
{
printf("File 1: Line n :%s \n",buf1);
printf("File 2: Line n: %s \n",buf2);
fprintf(fn3, "%s \n %s \n", buf1, buf2 );
}
} while ( !feof(fn1) || !feof(fn2))
// While file 1 or file 2 is not EOF
// With the files check print whats in the text file 3
if ( fn3 == NULL )
printf("The File was empty!!!");
else
printf("%s", fn3); // Print contents of txtfile 3 to screen
fclose(fn1);
fclose(fn2);
fclose(fn3);
return (0);
}
To me it seems as though it will work, but its the first time i've done this sort of prog of this sort of scope.
-ali
-
Do i need to clear the buf1 and buf2 once its finished reading in a line, not sure realisticaly you should but i can't tell. be easy to cleat it just fill it with nulls in a for loop
-
No
I think i answered this before. When you are dealing with strings and text files, you do not need to "clear the buffers". Using memset, bzero, or a for loop is a waste of time. All the C-string manipulation functions (i.e. strcmp) will ignore all of the bytes in the buffer after the '\0' character. So NULLing out the buffer isn't necessary.