使用 C 计算文本文件中的单词数

发布于 2024-12-20 14:36:07 字数 703 浏览 5 评论 0原文

嘿,我一直在尝试计算文本文件中的单词数,从 C 语言为 Hangman 游戏加载一堆单词,但我遇到了困难。我正在使用的这段代码假设我正在使用这段代码;

FILE *infile;
        FILE *infile;
char buffer[MAXWORD];
int iwant, nwords; 
iwant = rand() %nwords;

// Open the file

infile = fopen("words.txt", "r");

// If the file cannot be opened

if (infile ==NULL) {

    printf("The file can not be opened!\n");
    exit(1);
}

// The Word count

while (fscanf(infile, "%s", buffer) == 1) {

    ++nwords;
}

printf("There are %i words. \n", nwords);

    fclose(infile);
}

如果有人对如何解决这个问题有任何建议,我将非常感激。

文本文件每行1个字,共850个字。

应用了缓冲区建议,但字数统计仍然为 1606419282。

更正了放置

    int nwords = 0; 

工作!非常感谢!

Hey I have been trying to count the number of words in my text file, to load up a bunch of words for a Hangman game, from C but I am hitting a brick wall. This piece of code I am using is supposed I am using this piece of code;

FILE *infile;
        FILE *infile;
char buffer[MAXWORD];
int iwant, nwords; 
iwant = rand() %nwords;

// Open the file

infile = fopen("words.txt", "r");

// If the file cannot be opened

if (infile ==NULL) {

    printf("The file can not be opened!\n");
    exit(1);
}

// The Word count

while (fscanf(infile, "%s", buffer) == 1) {

    ++nwords;
}

printf("There are %i words. \n", nwords);

    fclose(infile);
}

If anyone has anyone has any suggestions on how to fix this I would be very grateful.

The text file has 1 word per line, with 850 words.

Applied the buffer suggestion, however the word count still came out at 1606419282.

The correction of putting

    int nwords = 0; 

Worked!! Thank you very much!

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

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

发布评论

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

评论(3

风和你 2024-12-27 14:36:07

那么这些词是每行一个条目吗?

while (fscanf(infile, "%s", &nwords) == 1); {
    ++nwords;
}

不做你认为它做的事。它读取 nwords 中的字符串,但它不是字符串。
如果你想这样做,那么你需要分配一个字符串,即 char buffer[XXX] ,它足够长以包含数据文件中最长的留置权并使用:

while (fscanf(infile, "%s", buffer) == 1) {
    ++nwords;
}

So the words are one entry per line?

while (fscanf(infile, "%s", &nwords) == 1); {
    ++nwords;
}

Doesn't do what you think it does. It reads a string in nwords, which isn't a string.
If you want to do it like this then you need to allocate a string ie char buffer[XXX] which is long enough to contain the longest lien in your data file and use:

while (fscanf(infile, "%s", buffer) == 1) {
    ++nwords;
}
不离久伴 2024-12-27 14:36:07

变量nwords从未被初始化。你不能假设它从零开始。

如果是这样,你会在下一行发生崩溃(“除以零”),其目的使我无法理解:

iwant = rand() %nwords;

所以,替换

int iwant, nwords; 
iwant = rand() %nwords;

int nwords = 0;

The variable nwords is never initialized. You cannot assume it to start out as zero.

If it were, you'd get a crash ("divide by zero") on the next line, whose purpose eludes me:

iwant = rand() %nwords;

So, replace

int iwant, nwords; 
iwant = rand() %nwords;

by

int nwords = 0;
七禾 2024-12-27 14:36:07
  1. 读取第一个单词和后面的空格后,您的 fscanf 返回输入缓冲区的空格。所以,下次你读 EMPTY 这个词时。
  2. 更改建议:

    fscanf(infile, "%s ", &buffer) // 注意空格!和&缓冲之前

    它将丢弃所有空格,直到下一个单词。它应该可以工作。


PS 最好不要使用 [f]scanf :-)

  1. After reading the first word and whitespace after it, your fscanf RETURNS to input buffer the whitespace. So, the next time you read EMPTY word.
  2. Change proposed:

    fscanf(infile, "%s ", &buffer) // notice the space!!! And & before buffer

    It will throw off ALL whitespace till the next word. It should work.


P.S. Better not use [f]scanf :-)

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