Modify the program below to use functions fgetc and fputs rather than getchar an
ID: 3587002 • Letter: M
Question
Modify the program below to use functions fgetc and fputs rather than getchar and puts. The program should give the user the option to read from the standard input and write to the standard output or to read from a specified file and write to a specified file. If the user chooses the second option, have the user enter the filenames for the input and output files. #include #define SIZE 80 int main(void) int c; l/ variable to hold character input by user char sentence [SIZE]// create char array int i = o; // initial i ze counter i // prompt user to enter line of text puts ("Enter a line of text:"); // use getchar to read each character while cciExplanation / Answer
#include<stdio.h>
#include<string.h>
#include<conio.h>
void main(){
Printf(Enter 1 if you want to read and write through std input or output and 2 to read and write form profile);
Scanf(%d, choice)
int choice ;
switch(choice)
{
case 1 :
printf("Choosen to read and write through STD input and STD output " );
stdConsole();
break;
case 2:
printf("Choosen to read and write through File system " );
fileOperations();break;
default:break;
}
}
{
void stdConsole() //Function for StdConsole input output
{
char str[80];
int i;
printf("Enter a string: ");
fgets(str, 10, stdin);
i = strlen(str)-1;
if(str[i] == ' ')
str[i] = '';
printf("This is your string: %s", str);
getch();
}
void fileOperations() //function For file operation
{
FILE *fp;
char str[];
fp = fopen("file.txt","w"); //Statement 1
if(fp == NULL)
{
printf(" Can't open file or file doesn't exist.");
exit(0);
}
do
{
gets(str);
fputs(str,fp);
}while(strlen(str)!=0);
printf(" Data written successfully...");
fclose(fp);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.