In: Computer Science
Write a C program using system call I/O to
a) open an existing text file passed to your program as a command line argument, then
b) display the content of the file,
c) ask the user what information he/she wants to append
d) receive the info from the user via keyboard
e) append the info received in
d) to the end of the file
f) display the updated content of the file
Code Screenshot :
Executable Code:
#include <stdio.h>
//Main Program
int main(int argc, char* argv[]){
//Opening the file
FILE* f = fopen(argv[1], "a+");
//Printing the existing content in the
file
printf("Existing content: \n");
char ch;
//Reading till the end of file
while((ch = fgetc(f)) != EOF) printf("%c", ch);
//Prompting the user for input
printf("Enter info to be appended: \n");
char content[100];
//Reading the input
fgets(content, 100, stdin);
//Writing to file
fprintf(f,"%s", content);
fseek(f,0,SEEK_SET);
//Printing contents of the file
printf("File after appending: \n");
while((ch = fgetc(f)) != EOF) printf("%c", ch);
fclose(f);
}
Sample Output :
Please comment
below if you have any queries.
Please do give a thumbs up if you liked the answer thanks
:)