读取具有不同行数的多个文件

Reading multiple files with different number of lines

我正在尝试从多个文件中读取数组,但每个文件中的数组大小不同。所以我所做的是,我尝试计算文件中的行数,然后将其存储为数组大小。

例如,我有两个 .txt 文件,File_1.txtFile_2.txt,其中包含以下数据:

0.000 300.00
0.054 2623.3
1.000 300.00
0.000 300.00
0.054 2623.3
0.500 1500.0
1.000 300.00

分别

这是我使用的代码:

int main()
{
    char filter[1024];
    char filename[60];
    FILE *fp;
    double *T_SR, Z_SR;

    for (int i = 1; i < 3; i++)
    {
        sprintf(filename, "File_%d.txt", i);
        fp = fopen(filename, "r");
        if (fp == NULL)
        {
            exit(1);
        }

        int count = 0;
        for (int j = getc(fp); j != EOF; j = getc(fp))
        {
            if (j == '\n')
            {
                count = count + 1;
            }
        }

        T_SR = (double *)malloc(count * sizeof(double));
        Z_SR = (double *)malloc(count * sizeof(double));

        for (int rows = 0; rows < count; rows++)
        {
            fscanf(fp, "%lf %lf", &Z_SR[rows], &T_SR[rows]);
            printf("%lf %lf\n", Z_SR[rows], T_SR[rows]);
            if (feof(fp))
            {
                break;
            }
        }
    }
}

但是它没有打印给定的数组作为输出,而是打印了这个:

0.0000 0.0000
0.0000 0.0000

我检查了count的值,很好。也许问题很简单,但我找不到它。有人可以帮忙吗?

在 运行 整个文件 getc 之后,文件指示符将位于文件末尾,您必须在使用 fscanf 之前将其设置回开头,您可以为此使用 rewind

rewind(fp); //<--
for (int rows = 0; rows < count; rows++)
{
    //...
}

除此之外,还存在其他问题 ,其中包括内存泄漏问题,以及您没有关闭文件或检查 malloc return, 这是您的代码的样子(带有注释):

double *T_SR, *Z_SR; // fix the pointer issue
//...
char line[1024]; // make sure it's larger than the largest line in the file

while (fgets(line, sizeof line, fp)) // fixes the count issue
{
    // doesn't count empty lines, if there are any
    if (line[0] != '\n')
    {
         count++;
    }
}
if(count > 0)
{
    T_SR = malloc(count * sizeof *T_SR);
    Z_SR = malloc(count * sizeof *Z_SR);
    if(T_SR == NULL || Z_SR == NULL) // check memory allocation
    {
        perror("malloc");
        return EXIT_FAILURE;
    }

    rewind(fp);
    for(int rows = 0; fscanf(fp, "%lf%lf", &Z_SR[rows], &T_SR[rows]) == 2; rows++)
    {
         printf("%lf %lf\n", Z_SR[rows], T_SR[rows]);
    }
    free(T_SR); // free the memory, avoids memory leaks
    free(Z_SR);
}
fclose(fp); // and close the file
//...

Live demo

有几个错误:

  1. 最重要的是 rewind 问题,已在 anastaciu 的回答中解决。
  2. double * T_SR, Z_SR错了,应该是double * T_SR, *Z_SR。我想知道你发布的代码是否是你编译的代码。
  3. 你的行数计算方法有问题。如果文件的最后一行不是以 \n 结尾,count 变量将为 2,您将错过最后一行。
  4. fscanf returns 阅读的项目数或 EOF。如果您检查过,您可能自己在代码中发现了问题。
  5. feof 检查完成得太晚,如果 fscanf 遇到 en EOF,您仍然打印由于 EOF 条件而未被读取的值。

I try to count the number of lines in the file and then store that as the array size.

除了关键的 rewind() 问题,避免阅读代码的一种方法是查找行数,另一种方法是查找 double。很容易获得与读取两个 double 的“行数”不匹配的行数。

使用一个方法找到两者。

size_t read_SR(size_t count, double *Z_SR, double *T_SR, FILE *inf) {
  char line[100];
  rewind(inf);
  size_t rows;
  while (fgets(line, sizeof line, inf)) {
    double Z, T; 
    if (sscanf(line, "%lf %lf", &Z, &T) != 2) return rows;
    if (rows < count) {
      if (Z_SR) Z_SR[rows] = Z;
      if (T_SR) T_SR[rows] = T;
    }
    rows++;
  }
  return rows;
}  

用法

// First pass, find size
size_t count = read_SR(0, NULL, NULL, inf);
double *T_SR = malloc(sizeof *T_SR * count);
double *Z_SR = malloc(sizeof *Z_SR * count);
// 2nd pass, save data
read_SR(count, Z_SR, T_SR, inf);