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.
我還要將所有三個文件中的每個單詞都放入一個2D數組(一個字符串數組的數組)中,然后對它們使用某種排序方法。
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.
謝謝你的幫助。
2
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 *。那顆星很重要,表明該類型是“指向文件的指針”。 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 while
loop.
每次調用函數時,它都會有一個新的count = 0,因此它將始終返回0或1.嘗試使用static int count;,使count成為一個全局變量,或者將當前計數傳遞給函數。您的另一個選擇是移動返回計數;在while循環之外的行。
You will also probably need to divide the count by two to get the number of words, using the format you posted.
您可能還需要將計數除以2以使用您發布的格式獲取單詞數。
2
It should be int countWords(FILE *f){
, with *
. And the return
statement should go before the last }
only, outside the loop.
它應該是int countWords(FILE * f){,帶*。並且return語句應該在循環之外的最后一個}之前。
0
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
本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2011/09/10/7255560ed4b20fef57cc3773df4b1d91.html。