you will read a list of numbers from a file, and print them to the screen in rev
ID: 3633313 • Letter: Y
Question
you will read a list of numbers from a file, and print them to the screen in reverse order. You will write this program in C.You will be given a file called numbers.txt which is known to contain at most 100 integers. You will read the numbers from this file into an array. When you are finished reading them, you will display them to the screen in reverse order.
as an example, numbers file
2
4
6
8
10
3
6
9
12
15
4
8
12
16
20
24
5
expected output
5
24
20
16
12
8
4
15
12
9
6
3
10
8
6
4
2
and if there is no input files the expected output is:
Unable to open file 'numbers.txt'
When reading integers in C from a file, you will use the fscanf function. This function will return the constant EOF if it is not successful in reading an integer. Use this knowledge to detect when you have reached the end of your input file.
Explanation / Answer
please rate - thanks
#include <stdio.h>
#include<stdlib.h>
int main()
{
FILE *input;
int a[100],i=0,j;
input = fopen("numbers.txt","r");
if(input == NULL)
{ printf("Unable to open file 'numbers.txt' ");
system("pause");
return 0;
}
while(fscanf(input,"%d",&a[i])>0&&i<100)
{
i++;
}
printf("numbers reversed ");
for(j=i-1;j>=0;j--)
printf("%d ",a[j]);
fclose(input);
getch();
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.