简体   繁体   English

使用C编程扫描文件并将内容存储在数组中

[英]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. 我想读取文件并将“ 83”存储在数组的空间3中。 Then store "2984" in Space 74 of the array. 然后将“ 2984”存储在阵列的空间74中。 Then "12" in space 8 of the array. 然后在数组的空间8中输入“ 12”。

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? 如何读取文件并忽略前面的“ S”并将其及其内容存储在该空间的数组中?

Thanks. 谢谢。

I came with 2 solution from this : 我从这里得到2解决方案:

1- fscanf() : as you can do the following 1- fscanf()您可以执行以下操作

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

that should do it ,it will skip the 'S' and take the values 应该这样做,它将跳过“ S”并取值

2- fgetc() : 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 只需使用fgetc()抛出该文件,并且每次找到'S'都忽略它并开始提取数据

i prefer the first solution , i suggest to read about fscanf() , fgetc() 我更喜欢第一个解决方案,我建议阅读fscanf()fgetc()

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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