Question: I need to read everything from file redirection into a new file using C. Here is what I have: int main(int argc,char *argv[]) { FILE

I need to read everything from file redirection into a new file using C. Here is what I have:

int main(int argc,char *argv[]) { FILE *fp; char temp_filename[] = "out.sp19";

if(argc == 1) // execute for file redirect {

printf("Getting data from file redirect. ");

fp = fopen("out.sp19", "w"); //create temp file to store data from command line.

if(!fp) // validation to make sure out.sp19 is created. { printf("Error opening file."); return 0; }

fprintf (fp,"%s ", stdin); //Print arguments from redirect to fp. fclose(fp);

}

This program executes ok, but the file "out.sp19" is empty.

Assuming the following:

1) The executable is named test.exe

2)The file being redirected is called redirect.txt

3) redirect.txt contains: apple banana orange

From the command line the program is executed like so: test < redirect.txt

All the contents from redirect.txt should then be written to out.sp19, but I am getting and empty file. Please help get the given code working.

Thanks!

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!