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

发布于 2024-09-11 22:54:06 字数 733 浏览 2 评论 0原文

大家好,

我是 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...,并且它们全部包含在单个文件中。

  1. 我想从每个文件中提取日期信息,在本例中为 2010-03-03,第 3 列和第 6 列。

  2. 求 3 和 6 各列的统计平均值。

  3. 然后将结果存储在一个仅包含日期的新文件中,以及上面各列的计算平均值以供进一步分析。

它们将用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.

  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.

  2. Find the statistical mean of the each of the columns of 3 and 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.

I am really counting or your assistance to this to get going on this.

Regards

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

や莫失莫忘 2024-09-18 22:54:06

这是一个起点:

#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;
}

您只需对每一行执行类似的操作即可。

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.

白芷 2024-09-18 22:54:06

sscanf 可以作为一个起点。
首先,您必须将每一行读入字符串中,并使用 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;
}

输出

Rudolph -> 12

可能有更好、更简单的方法来做到这一点,我相信会在 SO 这里得到答案。

编辑:

当然,您也可以使用 strtokstrtok_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.

/* 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).
Check the examples in the link to get an idea.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文