Write a program named “file_writer” (with the appropriate extension of .c/.cpp)
ID: 3689567 • Letter: W
Question
Write a program named “file_writer” (with the appropriate extension of .c/.cpp) that does the following:
-Asks the user to enter a file name (without spaces).
-Attempts to open the file with the name specified by the user (using the open system call) in write only mode.
-Displays an error message and quits the program if file open is unsuccessful. If file open is successful, Asks the user to enter a string (with no spaces) to write into the file, Writes the string specified by the user to the file (using the write system call), Closes the file (using the close system call).
Explanation / Answer
#include #include /* For exit() function */ int main() { char c[1000]; FILE *fptr; fptr=fopen("program.txt","w"); if(fptr==NULL) { printf("Error!"); exit(1); } printf("Enter a sentence: "); gets(c); fprintf(fptr,"%s",c); fclose(fptr); return 0; }Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.