I need help with my program-this is what it is required to do: 1.Open the file m
ID: 3535286 • Letter: I
Question
I need help with my program-this is what it is required to do:
1.Open the file myfile.txt (my not be the C: drive)
2.if(the file does not open properly)
-display an error
-drop to the bottom of the program and exit
3.else
-use a while loop to input each string(line of text) in the file
---display each line as you input it
-close the file
4.Thank the user for using your program
Here is what I have, but it's not working and I think I'm missing parts but I don't see it-PLEASE HELP!!
#include<stdio.h>
void main()
{
FILE *fp;
fp=fopen("myfile.txt", "r");
if(fp==NULL)
printf("File could not be opened");
char ch;
int i,pos;
clrscr();
fp=fopen("input.txt","r+");
fseek(fp,1,SEEK_END);
pos=ftell(fp);
i=0;
while(i<=pos)
{
ch=fgetc(fp);
printf("%c",ch);
fseek(fp,-i,SEEK_END);
i++;
}
return 0;
}
Explanation / Answer
#include<stdio.h>
void main()
{
FILE *fp;
fp=fopen("myfile.txt", "r");
if(fp==NULL)
{
printf("File could not be opened");
exit(0);// you have not exited here
}
char ch;
int i,pos;
clrscr();
fp=fopen("input.txt","r+");
fseek(fp,1,SEEK_END);
pos=ftell(fp);
i=0;
while(i<=pos)
{
ch=fgetc(fp);
printf("%c",ch);
fseek(fp,-i,SEEK_END);
i++;
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.