簡體   English   中英

C編程:一種計算文本文件中單詞數量的程序?

[英]C programming: a program that counts the number of words in a text file?

我正在嘗試實現對文本文件中的單詞數進行計數的功能。

到目前為止,這是我的嘗試。

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

int main()
{
  FILE *fp;
  char word[1000];
  int count = 0, i;
  int *ptr = NULL;

  printf("Enter filename: ");
  scanf("%s", word);
  fp = fopen(word, "r");

  while(fscanf(fp, "%s", word) != EOF) //dynamically allocate contents of the file into word
    ptr = (int *)malloc(sizeof(int));
  for(i = 0; i < 4000; i++)
  {
    if(word[i] == ' ')
      count++;
  }
  printf("Total: %d", count);
  return 0;
}//main

當我使用gcc-進行編譯時,會出現諸如“變量' ptr not not used”之類的錯誤 ,但是當我將文件內容動態分配到word[80]時,我認為我已經使用了它。

我認為我的單詞計數器存在嚴重問題……當明顯有200多個單詞時,它也會返回0。 有人可以啟發我嗎?

嗯,但是當我將文件的內容動態分配到word [80]時,我以為使用了它?

不,您一次又一次地設置它:

int *ptr = NULL;   // <-- pointer is set to null

while(fscanf(fp, "%s", word) != EOF) 
  ptr = (int *)malloc(sizeof(int)); // ptr is being set to some memory, again and again
                                    // also this could be a nice memory leak

因此,這就是為什么您有gcc告訴您“已設置變量'ptr'但未使用”的原因,因為您沒有使用它。

因此問題:

  1. ptr已設置但未使用
  2. 將(sizeof int)個字節分配給(int *)
  3. 內存泄漏,不斷覆蓋ptr
  4. fscanf()返回成功分配的數量,您應該使用該數量而不是EOF
  5. word[]長度為1000,但您循環到4000
  6. 通過將fscanf()結果放入“ word”中,您將不斷覆蓋其中的內容
  7. 您不應該轉換malloc()的返回值
  8. "%s"應該實際上是"%999s"以限制輸入的長度,但是如果有1000,我想您還是會安全的。

這就是我從頭頂上看到的所有內容,嘗試修復這些問題,然后看看可以到達的地方。

您在這里有很多錯誤。 fscanf(fp, "%s", word)將從文件中獲取一個新單詞,並在每次調用時將其存儲在單詞緩沖區中。 while循環沒有打開/關閉花括號,因此對於從文件中讀取的每個單詞,您都將分配一個新的int *。 從文件中讀取所有內容后,您將通過文件中最后一個單詞的字緩沖區並計算空格,但是您要迭代4000而不是1000。嘗試搜索“ C ++單詞數”而且我敢肯定,與我輸入此答案所需的時間相比,您將能夠在更短的時間內找到有效的解決方案。

不幸的是,您的程序有很多問題。 對於初學者,您在'word'上有一個索引溢出(已分配1000字節,但索引達到4000)。

為什么每次在while循環中讀取一個字符串時都分配一個整數?

您的程序應該看起來像這樣:

char buffer[1000];
int count = 0;
while(fscanf(fp, "%s", buffer) != EOF) count++;

編輯:對不起,我以為您正在閱讀字符,以上內容現在應該可以反映出更改。

從您的評論“將文件的內容動態分配到單詞中”來看,您似乎對代碼的實際作用有些困惑:

while(fscanf(fp, "%s", word) != EOF)
    ptr = (int *)malloc(sizeof(int));

實際上會重復調用fscanf ,直到返回EOF 盡管每個fscanf調用都從文件中讀取單詞並將其存儲到臨時緩沖區( word )中,但是此循環的主體沒有任何意義。 它動態分配足以容納1個整數的內存,並使ptr指向該內存(已分配但從未釋放的內存,這也會導致內存泄漏)。

您可以檢查fscanf的返回值是否等於1 ,因為此函數“返回成功匹配和分配的輸入項的數量” 您的while循環實際上應如下所示:

while(fscanf(fp, "%s", word) == 1)
    count++;

另請注意,您的char word[1000]; 定義了一個長度為1000的數組,但是您的for循環有4000次迭代,並且您試圖訪問超出數組范圍的元素,這導致未定義的行為 同樣, for循環的邏輯似乎寧願計算存儲在word的空格( ' ' )。 此循環對您根本沒有用,只需擺脫它即可。

希望這可以幫助 :)

#include <stdio.h>

int main()
{
  FILE *fp;
  int count = 0;
  char word[15], c;

  printf("Enter filename: ");
  scanf("%s", word);
  fp = fopen(word, "r");
  if(fp == NULL)
    return -1;

  while((c = fgetc(fp)) != EOF) {
    if(c == ' ')
      count++;
  }

  fclose(fp);
  printf("Total: %d", count+1);

  return 0;
}

這真的很簡單。

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

int main()
{
  FILE *fp;
  char word[1000];
  int count = 0, i;
/*  (why. What are you doing with this?)
  int *ptr = NULL;
*/

  printf("Enter filename: ");
  scanf("%s", word);
  fp = fopen(word, "r");

  while(fscanf(fp, "%s", word) != EOF) //dynamically allocate contents of the file into     word
count++;
/*
    ptr = (int *)malloc(sizeof(int));
  for(i = 0; i < 4000; i++)
  {
    if(word[i] == ' ')
      count++;
  }
*/

  printf("Total: %d \n", count);   // added a newline, always nice to end that way
  return 0;
}//main

我修改了您的內容只是為了向您展示取出的內容。 您可以通過對文件運行'wc'並從文件中獲取字數來驗證它在Linux上是否有效。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM