Write a C program that will open the file myfile.txt Verify that the file opened
ID: 3624808 • Letter: W
Question
Write a C program that will open the file myfile.txtVerify that the file opened properly.
If the file did not open properly, display an error message and drop to the bottom of the program and exit.
If the file did open properly use a while loop to input all the text in the file, displaying each line as you input it. You can detect the end of the file using feof (file end of file). While you output three lines of text in program L14b, this program should correctly handle any number of lines including zero.
Explanation / Answer
#include #include int main() { char c; FILE *fp; fp = fopen("myfile.txt", "r"); // fp = fopen("myfile.txt", "r"); if(fp == 0) { fprintf(stderr, "Error opening myfile.txt. "); exit(1); } while ((c = fgetc(fp)) != EOF){ printf("%c",c); } fclose(fp); 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.