Hi all,
I am trying to take in 2 input files and an output file in as command line arguments. I am new to this, so bear with me if the code is a little rough. Below is what I have on that.
In the command line I am typing in:
> a.out lab5_1.txt lab5_2.txt ( I'm not typing the > )
I get an error saying it can't open input file # 2. This is for a program that uses a struct and linked list. What I am trying to do is get the information from lab5_1.txt and lab5_2.txt and put it one line at a time into a.out. Can anyone see what I am doing wrong?
Code:
// driver
int main ( int argc, char *argv[] )
{
struct employee *list;
int flag = 1;
// create file variables
FILE *infile1, *infile2, *outfile;
char buffer[80];
char buffer2[80];
// check for errors in opening files
if ( argc < 2 )
{
fprintf (stderr, "Usage: %s filename\n", argv[0] );
exit (1);
}
if ( ( outfile = fopen ( argv[1], "w+" ) ) == NULL )
{
fprintf (stderr, "Can't create output file.\n" );
exit (1);
}
if ( ( infile1 = fopen( argv[2], "r" ) ) == NULL)
{
fprintf (stderr, "Can't open input file 1\n" );
exit (1);
}
strcpy( buffer, argv[2]);
if ( ( infile2 = fopen( argv[3], "r") ) == NULL )
{
fprintf (stderr, "Input file 2 wouldn't open\n" );
exit (1);
}
strcpy( buffer2, argv[3] );
while ( fread ( buffer, 80, 2, infile1 ) != '\n' )
fwrite ( buffer, sizeof ( struct employee ), 1, outfile );
if (fclose ( infile1 ) != 0 || fclose ( infile2 ) != 0
|| fclose ( outfile ) != 0 )
printf ( "Error in closing files\n" );
return (0);
}
Thank you for your help, Sandy