Can someone help with this C programming question Thanks You

Can someone help with this C programming question? Thanks

Your program will begin with the following command: a. out stations.txt lines.txt report.txt where a. out is the executable file; stations . txt is the input file; lines . txt and report. txt are output files. These files will be described later. Input file station s. txt contains metro station information of the following format Greenbelt 1 Green 0 Fort_Totten 3 Yellow 0 Green 700 Red 241 1 search metro station Your program should prompt the user to enter the name of the metro station. If it matches with one in the input file (case sensitive match), print out the information of the station just like it appears in the input file; if a match is not found, report not found. Write to the report. txt file either the search result such as Greenbelt 1 Green 0 or a message such as greenbelt not found!

Solution

#include <stdio.h>
#include <string.h>
int main( int argc, char *argv[] )
{
char filename[100];
char report[100];
char *p;
char metroname[200];
strcpy(filename, argv[1]); //input txt file
strcpy(report, argv[2]); //report text file
  
FILE *file = fopen ( filename, \"r\" ); //input txt file for read input data
FILE *fptr = fopen ( report, \"a+\"); //output txt file for report

printf(\"Enter Metro Station Name\ \");
scanf(\"%s\",metroname);

if ( file != NULL )
{
char line [ 128 ]; /* or other suitable maximum line size */
while ( fgets ( line, sizeof line, file ) != NULL ) /* read a line */
{
//fputs ( line, stdout ); /* write the line */

p=strstr( line , metroname); //check string in line of file
if(p!=NULL)
{
//printf(\"%s\",p);
fputs ( line, stdout ); /* write the line */
  
}
else
{
fputs ( metroname, fptr );
fputs (\"is nnot found\ \", fptr );
//fprintf(fptr,\"%s\", metroname);
//fprintf(fptr,\"%s\", \" is not found!../n\");
}
}
fclose ( file );
fclose ( fptr );
}
else
{
perror ( filename ); /* why didn\'t the file open? */
}
return 0;
}

Notes : compile the program,run program with commad line argumnets

example : program_name arg1 arg2 (arg1 is input file, arg2 is report file)

then output dispalyed if data found otherwise data is written into report file.

Can someone help with this C programming question? Thanks Your program will begin with the following command: a. out stations.txt lines.txt report.txt where a.
Can someone help with this C programming question? Thanks Your program will begin with the following command: a. out stations.txt lines.txt report.txt where a.

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site