C语言 如何使用 C 计算文本文件中的单词数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7374062/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I count the number of words in a text file using C?
提问by Michael Schilling
I need some help with a program that I am writing for my Systems Programming class. It is in C and I have very, very little experience with C. I need to merge three text file with the format:
我正在为我的系统编程课程编写一个程序,我需要一些帮助。它是用 C 语言编写的,我对 C 的经验非常非常少。我需要将三个文本文件与以下格式合并:
word1
word2
word3
...
wordX
I am also to bring each of the words from all three files and put them into a 2D array (an array of string-arrays), then use some sort of sorting method on them.
我还要将所有三个文件中的每个单词都放入一个二维数组(字符串数组的数组)中,然后对它们使用某种排序方法。
I shouldn't need help with the sorting, but I don't know how to get the word count from each of the text files or put them into an array.
我不需要排序方面的帮助,但我不知道如何从每个文本文件中获取字数或将它们放入数组中。
This is the function I have for counting the words in the file. It doesn't compile on gcc (probably for obvious reasons, but I don't know them). Do I even have the right idea?
这是我用于计算文件中单词的功能。它不会在 gcc 上编译(可能出于明显的原因,但我不知道)。我什至有正确的想法吗?
int countWords(FILE f){
int count = 0;
char ch;
while ((ch = fgetc(f)) != EOF){
if (ch == '\n')
count++;
//return count; originally here, but shouldn't be.
}
return count;
}
EDIT: I supposed I could just find a way to count the lines in the program, but I'm not sure if the approach would be any different from what I am trying to do here. (I have never really been that good at working with text files.
编辑:我想我可以找到一种方法来计算程序中的行数,但我不确定这种方法是否与我在这里尝试做的有任何不同。(我从来没有真正擅长处理文本文件。
Holy cow. I got it to count all of the lines in the program. I guess I'm a little rusty :P
天啊。我得到它来计算程序中的所有行。我想我有点生疏了:P
#include <stdlib.h>
#include <stdio.h>
int countWords(FILE *f){
int count = 0;
char ch;
while ((ch = fgetc(f)) != EOF){
if (ch == '\n')
count++;
}
return count;
}
int main(void){
int wordCount = 0;
FILE *rFile = fopen("american0.txt", "r");
wordCount += countWords(rFile);
printf("%d", wordCount);
return 0;
}
I kind of forgot about that the pointer thing with FILE *fileName
我有点忘记了 FILE *fileName 的指针
Thanks for the help guys.
谢谢你们的帮助。
采纳答案by Borealid
The type you use for a file in c is FILE*. That star is important, indicating that the type is a "pointer to FILE". It is unlikely that countWords(FILE f)is what you meant to write.
您在 c 中用于文件的类型是FILE*. 那颗星很重要,表明该类型是“指向 FILE 的指针”。这不太可能countWords(FILE f)是您要写的内容。
Each time you call your function, it will have a fresh count = 0, so it will always return 0 or 1. Try using static int count;, making count a global variable, or passing in the current count to the function. Your other option is to move the return count;line outside of the whileloop.
每次调用函数时,它都会有一个新的count = 0,因此它总是返回 0 或 1。尝试使用static int count;,将 count 设置为全局变量,或将当前计数传递给函数。您的另一个选择是将return count;线移到while循环之外。
You will also probably need to divide the count by two to get the number of words, using the format you posted.
您可能还需要使用您发布的格式将计数除以二以获得字数。
回答by Tom Zych
It should be int countWords(FILE *f){, with *. And the returnstatement should go before the last }only, outside the loop.
应该是int countWords(FILE *f){,与*。并且该return语句应该}只在最后一个之前,在循环之外。
回答by Megharaj
Here is the code. Just read the number of spaces, that it.
这是代码。随便读个空格数,就知道了。
#include<stdio.h>
#define FILE_READ "file.txt"
int main()
{
FILE * filp;
int count = 1;
char c;
filp = fopen(FILE_READ, "r");
if(filp == NULL)
printf("file not found\n");
while((c = fgetc(filp)) != EOF) {
if(c == ' ')
count++;
}
printf("worrds = %d\n", count);
return 0;
}
text file
文本文件
I am megharaj, from india.
output,
输出,
worrds = 5

