简体   繁体   English

如何从c中的文件中读取特定数据列

[英]How can I read specific data columns from a file in 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. 我是c编程的初学者。我遇到了这个问题并且已经花了相当多的时间在它上面而没有任何可观的进展。

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. 我有一系列扩展名为(.msr)的文件,它们包含十个参数的测量数值,这些参数的范围从日期,时间,温度,压力......,用分号分隔。 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. 每个文件都有一个名称REG_2010-03-03,REG_2010-03-04,REG_2010-03-05,......它们都包含在一个文件中。

  1. I want to extract from each of the file the date information which in this case 2010-03-03, column 3 and column 6. 我想从每个文件中提取日期信息,在本例中为2010-03-03,第3列和第6列。

  2. Find the statistical mean of the each of the columns of 3 and 6. 找到3和6的每列的统计平均值。

  3. 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. 它们用c写成。

I am really counting or your assistance to this to get going on this. 我真的在计算或者你的帮助,以便继续这样做。

Regards 问候

Here is a starting point: 这是一个起点:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
    char str[] = "2010-03-03 15:55:06; 8.01; 24.9; 14.52; 0.09; 84; 12.47;";
    char *date, *tmp;
    double mean = 0;
    int i;

    date = strtok(str, " ");
    strtok(NULL, " "); // skip over time
    while (tmp = strtok(NULL, ";")) {
        ++i;
        if (i == 3 || i == 6) { // get only the 3rd and 6th value
            mean += strtod(tmp, NULL);
        }
    }
    mean /= 2;

    printf("%s: %.2f\n", date, mean);

    return 0;
}

You'll just have to do something like that to each line. 你只需要为每一行做类似的事情。

sscanf can be a start point. sscanf可以作为一个起点。 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. 首先,您必须将每一行读入一个字符串并使用sscanf来获取所需的参数,这些参数由该字符串中的空格分隔。

/* sscanf example */
#include <stdio.h>

int main ()
{
  char sentence []="Rudolph is 12 years old";
  char str [20];
  int i;

  sscanf (sentence,"%s %*s %d",str,&i);
  printf ("%s -> %d\n",str,i);

  return 0;
}

Output 产量

Rudolph -> 12

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). 当然,你也可以使用strtokstrtok_r (strtok的可重入版本)解析每个字符串。 Check the examples in the link to get an idea. 检查链接中的示例以获得一个想法。

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

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