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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started