How do I count the number of words in a text file using C?
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:
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?
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.
I got it to count all of the lines in the program. I guess I'm a little rusty开发者_开发知识库.
#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
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.
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 while
loop.
You will also probably need to divide the count by two to get the number of words, using the format you posted.
It should be int countWords(FILE *f){
, with *
. And the return
statement should go before the last }
only, outside the loop.
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
here's a code from my uni book
#include <stdio.h>
int main(void){
FILE *f = fopen("file.txt", "r");
int count = 0, word=0;
char ch;
while ((ch = fgetc(f)) != EOF){
if (ch == ' ' || ch == '\n') {
word = 0;
} else {
if(!word){
count++;
word = 1;
}
}
}
printf("%d", count);
return 0;
}
精彩评论