我正在嘗試從文本文件創建一個單詞數組。我能夠得到它打印出正確的值,但我需要一個實際上可以使用的數組。有了這個數組之後,我必須對所存儲的單詞進行各種操作,例如統計每個單詞的長度。現在我只需要幫助製作一個我可以實際使用的數組。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