简体   繁体   中英

Scan file and store contents in an array in C programming

I have a file with the following contents:

S3 83
S74 2984
S8 12
... and so on for x # of values

The first value tells you which space to store and the second number tells you the content.

For example:

I want to read the file and store "83" in Space 3 of an array. Then store "2984" in Space 74 of the array. Then "12" in space 8 of the array.

How do I read the file and ignore the "S" at the front and store it in that Space in the array with its contents?

Thanks.

I came with 2 solution from this :

1- fscanf() : as you can do the following

fscanf(fp, "S%d %d",&slot, &val);

that should do it ,it will skip the 'S' and take the values

2- fgetc() :

simply you can go throw the file with fgetc() and every time you find an 'S' ignore it and start extract data after it

i prefer the first solution , i suggest to read about fscanf() , fgetc()

https://www.tutorialspoint.com/c_standard_library/c_function_fgetc.htm https://www.tutorialspoint.com/c_standard_library/c_function_fscanf.htm

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM