Create a file and call it testdata2 The file should contain
Create a file and call it testdata2. The file should contain an unknown number of integer values. Write a C program that uses a while loop to read in each number and then prints the number back onto the screen. The Boolean expression of the while loop must contain a call to the fscanf function. You need to read your textbook. Look at page 63. Study the code there. Make certain you understand why it is this way.
Solution
#include<stdio.h>
int main(void)
{
int i;
FILE *file;
if( (file = fopen(\"testdata2.txt\", \"r+\")) == NULL)
{
printf(\"No such file\ \");
exit(1);
}
if (file == NULL)
{
printf(\"Error Reading File\ \");
}
/* fscanf returns the length of characters read successfully. */
while(fscanf(file,\"%d\",&i) == 1)
{
printf(\"\ %d\",i);
}
return 0;
}
