如何从c中的文件中读取特定数据列
大家好,
我是 C 编程的初学者。我遇到了这个问题,并且在上面花费了大量时间,但没有取得任何重大进展。
我的问题是这样表述的:
我有一系列扩展名为(.msr)的文件,它们包含十多个参数的测量数值,这些参数的范围包括日期、时间、温度、压力......冒号。 数据值的示例如下所示。
2010-03-03 15:55:06; 8.01; 24.9; 14.52; 0.09; 84; 12.47;
2010-03-03 15:55:10; 31.81; 24.9; 14.51; 0.08; 82; 12.40;
2010-03-03 15:55:14; 45.19; 24.9; 14.52; 0.08; 86; 12.32;
2010-03-03 15:55:17; 63.09; 24.9; 14.51; 0.07; 84; 12.24;
每个文件的名称为 REG_2010-03-03、REG_2010-03-04、REG_2010-03-05...,并且它们全部包含在单个文件中。
我想从每个文件中提取日期信息,在本例中为 2010-03-03,第 3 列和第 6 列。
求 3 和 6 各列的统计平均值。
然后将结果存储在一个仅包含日期的新文件中,以及上面各列的计算平均值以供进一步分析。
它们将用c 编写。
我真的很希望得到您的帮助才能继续下去。
问候
Good day all,
I am a beginner in c programming.I have this problem and have have spent quite a huge amount of time on it without any considerable progress.
My problem is stated thus:
I have a series of files with the extension (.msr), they contain measured numerical values of more that ten parameters which ranges from date,time,temperature, pressure, .... that are separated by semi colon.
The examples of the data values are shown below.
2010-03-03 15:55:06; 8.01; 24.9; 14.52; 0.09; 84; 12.47;
2010-03-03 15:55:10; 31.81; 24.9; 14.51; 0.08; 82; 12.40;
2010-03-03 15:55:14; 45.19; 24.9; 14.52; 0.08; 86; 12.32;
2010-03-03 15:55:17; 63.09; 24.9; 14.51; 0.07; 84; 12.24;
Each of the files have as a name REG_2010-03-03,REG_2010-03-04,REG_2010-03-05,... and they are all contained in a single file.
I want to extract from each of the file the date information which in this case 2010-03-03, column 3 and column 6.
Find the statistical mean of the each of the columns of 3 and 6.
Then store the results in a new file which will only contain the date,and the calculated mean of the columns above for further analysis.
They are to be written in c.
I am really counting or your assistance to this to get going on this.
Regards
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个起点:
您只需对每一行执行类似的操作即可。
Here is a starting point:
You'll just have to do something like that to each line.
sscanf 可以作为一个起点。
首先,您必须将每一行读入字符串中,并使用 sscanf 获取所需的参数,这些参数在该字符串中以空格分隔。
输出
可能有更好、更简单的方法来做到这一点,我相信会在 SO 这里得到答案。
编辑:
当然,您也可以使用 strtok 或 strtok_r(strtok的可重入版本)。
检查链接中的示例以获得一个想法。
sscanf can be a start point.
First you will have to read every line into a string and use sscanf to get the required parameters which are separated by a whitespace in that string.
Output
There may be better and easier ways to do it which i am sure will be answered here at SO.
EDIT :
Ofcourse you can also parse each string using strtok or strtok_r(reentrant version of strtok).
Check the examples in the link to get an idea.