簡體   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