Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - How to open a file with fopen and read its contents into an integer array?
How to open a file with fopen and read its contents into an integer array?
Hello, the following program demonstrates file opening, file pointer judgment, data reading and file closing. The program debugging has passed, and I hope it will be helpful to you!

# include & ltstdio.h & gt

Master ()

{

FILE * fp

int i,j,a[ 10];

If ((FP = fopen ("data.txt ","r+b")) = = null) {/* Open the data file data.txt */

Printf ("Unable to open file. \ n ");

Exit (0);

}

I = 0;

while(feof(FP)= = 0 & amp; & amp & lt80){ /* Read integers one by one until the end of the file or more than 80 integers */

j=fscanf(fp," %d ",& ampa[i++]);

}

fclose(FP); /* Close file pointer */

Printf ("data is: \ n");

for(j = 0; j & ltI- 1; j++) printf("%d ",a[j]); /* Output the read data to the screen */

}