繁体   English   中英

如何获取文件的长度

[英]How to obtain the length of a file

我正在尝试运行一个简单的 C 程序,该程序采用随机浮点值的文件,自动识别文件的长度并使用该长度执行进一步的计算。 但是,我的编译器要么挂起,要么得到错误的结果。 这是我的代码

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <math.h>

int main() {
  FILE *fptr;
  int count = 0; // Line counter (result)
  char ch; // To store a character read from file

  if ((fptr = fopen("C:\\Users\\Evandovich\\Desktop\\White_Noise.txt", "r"))
      == NULL) {
    printf("Error! opening file");
    // Program exits if the file pointer returns NULL.
    exit(1);
  }

  // Extract characters from file and store in character ch
  for (ch = getc(fptr); ch != EOF; ch = getc(fptr)) {
    if (ch == '\n') // Increment count if this character is newline
      count = count + 1;
  }
  printf("The file has %d lines\n ", count);

  // use the value of "count" to be the length of the array.
  char arrayNum[count];
  char *eptr;
  double result, result1[count];

  for (int i = 0; i < count; i++) {
    fscanf(fptr, "%s", &arrayNum[i]);

    /* Convert the provided value to a double */
    result = strtod(&arrayNum[i], &eptr);
    result1[i] = pow(result, 2);
    printf("value %f\n", result1[i]);
  }

  fclose(fptr);
  return 0;
}

错误具体是什么? 非常感谢您的意见。

输入文件 (N.txt) 包含

0.137726
0.390126
-0.883234
0.006154
-0.170388
-1.651212
0.510328

OUTPUT 该文件有7个文件

value 0.000000
value 0.000000
value 0.000000
value 0.000000
value 0.000000
value 0.000000
value 0.000000

预期该文件有7个文件

value 0.018968
value 0.152198
value 0.780102
value 0.000038
value 0.029032
value 2.726501
value 0.260435

至少这些问题:

在文件末尾

代码失败,因为它试图从文件末尾读取浮点文本。 @ErlingHaaland

确定行数后,添加:

 rewind(fptr);

错综复杂的阅读

使用fgets()读取一行。 避免使用没有宽度限制"%s" ——它可能会溢出。 使用基于最大行长度而非行数的行缓冲区。 从行首开始转换为double

#define LINE_SIZE 100
char arrayNum[LINE_SIZE];
if (fgets(arrayNum, sizeof arrayNum, fptr) == NULL) {
  break;
}
result = strtod(arrayNum, &eptr);

检查转换

errno = 0;
result = strtod(arrayNum, &eptr);
if (arrayNum == eptr || errno) {
  break;
}

类型太小

int getc(FILE *)通常返回 257 个不同的值: EOF和 [0... UCHAR_MAX ]。 将其保存在char中会丢失信息。 保存在一个int中。

行数有风险

可能会相差 1,因为最后一行可能没有'\n'@Adrian McCarthy

而是计算行的开头

size_t count = 0;
int previous = '\n';
int ch;

while ((ch = getc(fptr) != EOF) {
  if (previous == '\n') {
    count++;
  }
  previous = ch;
}
printf("The file has %zu lines.\n ", count);

// Also
rewind(fptr);

几个问题:

  1. getc将下一个char的值作为int或特殊值EOF返回。 由于您的变量chchar ,因此EOF值可能无法识别,在这种情况下,行计数循环可能永远不会结束。

  2. 您定义 arrays ,其大小由运行时变量count确定。 我经常使用 C++,但我已经在很长时间内写过 C。 当我写C时,arrays的大小必须在编译时确定。 也许这是 C 的新功能? 我会检查。 确保您启用了编译器警告。

  3. 如果文件的最后一行不以 '\n' 结尾,则count可能会缩短一行。 在 Posix 系统上,一行应该以换行符结尾。 在 Windows 上,换行序列(通常是 CR+LF)通常被视为行分隔符。 因此,文件可能会也可能不会以最后一行末尾的换行符序列结束。

  4. arrayNum是一个count字符数组,而不是一个指向字符串的指针数组。

获取文件大小的最快、最有效和最安全的方法是通过stat()询问操作系统:

struct stat statbuf;
stat("C:\\Users\\Evandovich\\Desktop\\White_Noise.txt", &statbuf);
statbuf.st_size; /* The file size in bytes */

暂无
暂无

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

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