Here I want you to be able to read a file which has titles of magazine articles
ID: 3642782 • Letter: H
Question
Here I want you to be able to read a file which has titles of magazine articles and the page number they occur on in it.I want you to ask the user for a page number, and give them the article which appears on that page number.
For example.
example.txt
10 The First Article
24 The Article After the first article
33 The Third Article
40 The Forth Article
54 The Last Article
program
>> Enter a page number
24
>> "The Article After the first article" starts on that page number
program
>> Enter a page number
65
>> No Article starts on that page number
Explanation / Answer
#include #include #include int main() { int n; printf("Enter a page number "); scanf("%d",&n); FILE *myfile; myfile = fopen("input.txt","r"); char *buffer; char *temp; temp=(char *)malloc(100*sizeof(char)); buffer=(char *)malloc(100*sizeof(char)); int t=0; int n1=n; int i,j; int found=0; while(fgets(buffer,100,myfile)) { t=0; n=n1; temp=buffer; while((*buffer)!=' ') { t++; buffer++; } buffer++; for(i=t-1;i>=0;i--) { if((int)temp[i]-48!=n%10) break; n=n/10; } if(i==-1) { printf("%sstarts on that page number ",buffer); found=1; break; } } if(found==0) printf("No article starts on that page number "); }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.