I need help with source code modifying a problem For this program, you are going
ID: 3532776 • Letter: I
Question
I need help with source code modifying a problem
For this program, you are going to modify your previous program (program 3) so that it will now have a menu to see if the user wants to read the input from a file or interactively. If the user wants to read the input from a file, then the output will also go into a different file. If the user wants to read the input interactively, then the output will go to the screen.
Your initial menu will look like the following:
Menu
<1> Read data from a file
<2> Read data interactively
Enter choice:
Here is a sample input file:
21 25
30 30
10 12
55 60
-1 /* this is the flag to end inputting the assignment data */
21 25
30 30
10 12
21 25
30 30
Here is another sample input file:
21 25
30 30
10 12
55 60
-1 /* this is the flag to end inputting the assignment data */
21 25
-1 /* this is the flag to end inputting the midterm exam data */
21 25
30 30
Explanation / Answer
I have checked and verified without any errors
#include <stdio.h>
int main()
{
printf(" Menu <1> Read data from a file <2> Read data interactively Enter choice:");
int choice;
scanf("%d",&choice);
char fread[100];
char fwrite[100];
FILE* fp1,*fp2;
if(choice == 1)
{
printf("Enter the input filename: ");
scanf("%s",fread);
fp1 = fopen(fread,"r");
printf("Enter the output filename: ");
scanf("%s",fwrite);
}
else
{
if(choice == 2)
{
fp1 = stdin;
fp2 = stdout;
}
else
{
printf("Incorrect choice ");
return 0;
}
}
//Put your code here
/* Here you can take input as :
fscanf(fp1,"%d",&in); exactly same as scanf except for fp1 as the first argument
Here you can give output as :
fprintf(fp2,"%d",in); exactly same as printf except for fp2 as the first argument
No need to worry about anything else such as file or keyboard or screen
*/
fclose(fp1);
fclose(fp2);
return 1;
}