我正在尝试从文本文件创建一个单词数组。我能够得到它打印出正确的值,但我需要一个实际上可以使用的数组。有了这个数组之后,我必须对所存储的单词进行各种操作,例如统计每个单词的长度。现在我只需要帮助制作一个我可以实际使用的数组。C从TexFile创建数组
下面是代码:
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
int main (int argc, char* argv[]){
// First Read in First novel File
FILE *fp;
char *ProgFile;
// Variables for Parsing
int i = 0;
int j=0;
char *cp;
char *bp;
char line[255];
char *array[5000];
int x;
int wordCount=0;
int wordCountPerNovel;
// Adjusting the file name to include txt and corresponding number
strcat(argv[1],"_1.txt");
ProgFile = argv[1];
// Open Each File
fp=fopen(ProgFile,"r");
if(fp==NULL)printf("error");
else printf("bin file loaded: '%s'",ProgFile);
// Now begin analysing
// Part 1
// Parse Entire Document into Array of Strings
while (fgets(line, sizeof(line), fp) != NULL) {
bp = line;
while (1) {
cp = strtok(bp, ",.!?<97> \n");
bp = NULL;
if (cp == NULL)break;
array[i++] = cp;
printf("Check print - word %i:%s:\n",i-1, cp);
}
}
// At this point i is the last word that was iterated, -1 since it breaks out after being added
// This gets total words of all novels
wordCount=wordCount+(i-1);
printf("\nTotal words %i\n",wordCount);
// Find Total number of letters
//for (i=1;i<15;i++){
// printf("My value: %s \n",finalArrayWord[i]);
//
//}
你居然没问一个问题。 – melpomene
什么是一个*数组,我实际上可以使用*? – Arash
1)'strcat(argv [1],“_ 1.txt”);'不能这样。 – BLUEPIXY