如何读取存储在目录中的不同文件并将其中的一些数据存储到一个文件

发布于 2024-09-12 07:51:48 字数 2900 浏览 1 评论 0原文

这是我之前提出的问题的后续,在一些人的帮助下,我能够开始编写我想要编写的函数,但我尚未完成它。 这是我之前的问题: 我有一系列扩展名为(.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.然后将结果存储在一个新文件中,该文件仅包含日期和上面各列的计算平均值以供进一步分析。

我现在的问题是: 我希望能够打开包含 30 个扩展名为 .msr 的文件的目录。我想打开源文件,然后对于其中的每个文件,提取所需的信息,正如我之前解释的那样,并为上面读取的每个文件存储日期(每个文件中统一)以及第 3 列和第 6 列的平均值因此,目标文件每行将包含三列,即日期、平均值(第 3 列)和平均值(第 6 列),以空格分隔,总共 30 行。下面是我开始使用的代码,希望您能指导我如何实现它。

正如您上面概述的那样。 这是我想要实现的概述

1) 打开包含文件的目录(这里是 USB KEY)。 2)读取里面所有的msr文件名。 3)打开每个msr文件。 4)提取日期(文件中的第一列),忽略时间和分隔符( 5)提取数据1(第3列数据) 6)提取数据2(第6列数据) 7) 计算第三列和第六列的平均值。 8)输出到文件(日期,平均第3列,平均第6列) 9) 关闭msr文件 10)关闭目录(如果可能)

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

int file_getline_analyse(char *infile,char *outfile,char *path,char *strline) {

int return_value=0;

    FILE *fd=NULL;    // pointer for data source
    FILE *fo= NULL;   // Destination file
    char *file_path=NULL;     

    char *date, *tmp,*time;
    double sum, mean = 0;
    file_path=calloc((strlen(path)+strlen(infile)),sizeof(file_path));   
    if (file_path==NULL) {
        printf("file_path in get_line\n");
        exit(EXIT_FAILURE);
    }

    strcpy(file_path,path);    // copies the path entered in the function call to the allocated meomory 
    strcat(file_path,infile);  // concatenates the contents of the  allocated meomory from the source file

    fd=fopen(file_path,"r");

    fo = fopen(outfile, "w");

    if((fd==NULL) && (fo==NULL))  {
        return_value = -1;
    }
    else {
        int i=0;
        int j=0;
        while ((fgets (strline, BUFSIZ, fd))>0){
            date = strtok(strline, " ");
            time=strtok(NULL, " "); // skip over time
            tmp = strtok(NULL, ";");
            if (i == 3|| i == 6) { // get only the 3rd and 6th value
                sum += strtod(tmp, NULL);
                ++i;
                if(j== '\n') {
                    // Replacing the characters at the end of the line by 0:
                    char *p = strchr (strline, '\n');
                    if (p) {
                        *p = 0;
                    }
                    return_value = 0;
                    break;

                }
                j++;


            }

            mean = sum/(double)(j+1);

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

        }
        fclose (fd);
        fclose(fo);
    }

    free(file_path);
    file_path=NULL;

    return return_value;
}

This is a follow up to the question I asked earlier and with the help of some people here I was able to start up with the function I want to write,but I am yet to complete it.
Here is my earlier question:
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.

My question now:
I want to to be able to open the directory which contains 30 files with extension of .msr . I want to open the source file, then for each file inside it, to extract the informations needed as I have explained earlier and for each file read above to store the date (uniform in each file) and the mean value of column 3 and 6 in a single file.Thus the destination file will contain at each line three columns which are the date, mean(3rd column) and mean(6th column) separated by space making it a total of 30 rows. Below is the code I started with and would appreciate your guide on how to implement this.

just as you outlined above.
Here is the outline of what I want to achieve

1) Open the directory that contains the files(here is USB KEY).
2) Read all the msr filenames inside it.
3) Open each msr files.
4) Extract the date (its the first column in the file),ignore the time and the separator(
5) extract data 1 (data at the 3rd column)
6) extract data 2 (data at the 6th column)
7) Calculate the mean for 3rd column and 6th column.
8) output to file (date,mean 3rd column,mean 6th column)
9) close msr files
10) close the directory(if possible)

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

int file_getline_analyse(char *infile,char *outfile,char *path,char *strline) {

int return_value=0;

    FILE *fd=NULL;    // pointer for data source
    FILE *fo= NULL;   // Destination file
    char *file_path=NULL;     

    char *date, *tmp,*time;
    double sum, mean = 0;
    file_path=calloc((strlen(path)+strlen(infile)),sizeof(file_path));   
    if (file_path==NULL) {
        printf("file_path in get_line\n");
        exit(EXIT_FAILURE);
    }

    strcpy(file_path,path);    // copies the path entered in the function call to the allocated meomory 
    strcat(file_path,infile);  // concatenates the contents of the  allocated meomory from the source file

    fd=fopen(file_path,"r");

    fo = fopen(outfile, "w");

    if((fd==NULL) && (fo==NULL))  {
        return_value = -1;
    }
    else {
        int i=0;
        int j=0;
        while ((fgets (strline, BUFSIZ, fd))>0){
            date = strtok(strline, " ");
            time=strtok(NULL, " "); // skip over time
            tmp = strtok(NULL, ";");
            if (i == 3|| i == 6) { // get only the 3rd and 6th value
                sum += strtod(tmp, NULL);
                ++i;
                if(j== '\n') {
                    // Replacing the characters at the end of the line by 0:
                    char *p = strchr (strline, '\n');
                    if (p) {
                        *p = 0;
                    }
                    return_value = 0;
                    break;

                }
                j++;


            }

            mean = sum/(double)(j+1);

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

        }
        fclose (fd);
        fclose(fo);
    }

    free(file_path);
    file_path=NULL;

    return return_value;
}

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

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

发布评论

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

评论(1

霓裳挽歌倾城醉 2024-09-19 07:51:48

如果你不需要它在 C 中,我会选择另一种语言,例如 Perl:

sub analyze($) {
  my ($fname) = @_;
  my ($date, $sum3, $sum6, $n) = (undef, 0, 0, 0);

  open(F, "<", $fname) or die "$fname: $!";
  while (defined(my $line = <F>)) {
    my @words = split(m";", $line);
    $date = split(" ", $words[0])[0]; # only use the date, not the time
    $sum3 += $words[2];
    $sum6 += $words[5];
    $n++;
  }
  close(F) or die "$fname: $!";
  printf("%s;%f;%f\n", $date, $sum3 / $n, $sum6 / $n);
}

foreach my $fname (@ARGV) {
  analyze($fname);
}

在 C 中,你缺少这样方便的功能,例如:

  • 自动内存管理
  • 对字符串的简单支持,例如连接、拆分

If you don't need it to be in C, I would choose another language, for example Perl:

sub analyze($) {
  my ($fname) = @_;
  my ($date, $sum3, $sum6, $n) = (undef, 0, 0, 0);

  open(F, "<", $fname) or die "$fname: $!";
  while (defined(my $line = <F>)) {
    my @words = split(m";", $line);
    $date = split(" ", $words[0])[0]; # only use the date, not the time
    $sum3 += $words[2];
    $sum6 += $words[5];
    $n++;
  }
  close(F) or die "$fname: $!";
  printf("%s;%f;%f\n", $date, $sum3 / $n, $sum6 / $n);
}

foreach my $fname (@ARGV) {
  analyze($fname);
}

In C, you are missing such handy features like:

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