C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 08-14-2001, 04:38 PM   #1
Registered User
 
Join Date: Aug 2001
Posts: 27
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
@licomb is offline   Reply With Quote
Old 08-14-2001, 04:50 PM   #2
zen
of Zen Hall
 
zen's Avatar
 
Join Date: Aug 2001
Posts: 1,007
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)
zen is offline   Reply With Quote
Old 08-14-2001, 04:51 PM   #3
Registered User
 
Join Date: Aug 2001
Posts: 27
I've just noticed i haven't closed all the files,will add that to end of program.
@licomb is offline   Reply With Quote
Old 08-14-2001, 05:02 PM   #4
Registered User
 
Join Date: Aug 2001
Posts: 27
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
@licomb is offline   Reply With Quote
Old 08-14-2001, 05:11 PM   #5
zen
of Zen Hall
 
zen's Avatar
 
Join Date: Aug 2001
Posts: 1,007
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.
zen is offline   Reply With Quote
Old 08-14-2001, 05:52 PM   #6
Registered User
 
Join Date: Aug 2001
Posts: 27
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.
@licomb is offline   Reply With Quote
Old 08-14-2001, 06:14 PM   #7
zen
of Zen Hall
 
zen's Avatar
 
Join Date: Aug 2001
Posts: 1,007
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.
zen is offline   Reply With Quote
Old 08-14-2001, 06:19 PM   #8
Registered User
 
Join Date: Aug 2001
Posts: 27
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
@licomb is offline   Reply With Quote
Old 08-14-2001, 06:19 PM   #9
zen
of Zen Hall
 
zen's Avatar
 
Join Date: Aug 2001
Posts: 1,007
........and you've written NULL as Null.

and I'm now a member
zen is offline   Reply With Quote
Old 08-14-2001, 06:24 PM   #10
Registered User
 
Join Date: Aug 2001
Posts: 27
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
@licomb is offline   Reply With Quote
Old 08-14-2001, 06:26 PM   #11
zen
of Zen Hall
 
zen's Avatar
 
Join Date: Aug 2001
Posts: 1,007
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.
zen is offline   Reply With Quote
Old 08-14-2001, 06:33 PM   #12
Registered User
 
Join Date: Aug 2001
Posts: 27
I would never of thought to use that thanks
@licomb is offline   Reply With Quote
Old 08-14-2001, 06:53 PM   #13
Registered User
 
Join Date: Aug 2001
Posts: 27
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
@licomb is offline   Reply With Quote
Old 08-14-2001, 07:36 PM   #14
Registered User
 
Join Date: Aug 2001
Posts: 27
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
@licomb is offline   Reply With Quote
Old 08-14-2001, 10:04 PM   #15
junior member
 
mix0matt's Avatar
 
Join Date: Aug 2001
Posts: 144
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.
__________________
THIS IS NOT JUST A CHRONICLING OF THINGS WE HAVE DONE IN THE PAST BUT OUR RISE TO POWER.
mix0matt is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
program quits unexpected skelesp C Programming 34 12-10-2008 09:10 AM
Issue with program that's calling a function and has a loop tigerfansince84 C++ Programming 9 11-12-2008 01:38 PM
Need help with a program, theres something in it for you engstudent363 C Programming 1 02-29-2008 01:41 PM
This is a simple program.. Help me please I think it has an error.. lesrhac03 C Programming 4 02-21-2008 10:39 AM


All times are GMT -6. The time now is 09:51 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22