2017-04-07 62 views
0

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

你居然沒問一個問題。 – melpomene

+0

什麼是一個*數組,我實際上可以使用*? – Arash

+0

1)'strcat(argv [1],「_ 1.txt」);'不能這樣。 – BLUEPIXY

回答

1

的代碼沒有編譯。如果你知道長度,你可以使用char數組,並且你不必使用malloc從文件讀入數組。

文件1.txt的

Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatu 

代碼

#include <stdio.h> 
#include <string.h> 
#include <stdlib.h> 

void read_words (FILE *f) { 
    char x[1024]; 
    char **a; 
    a = malloc(1024 * sizeof(char*)); 
    for (int i = 0; i < 1024; i++) 
     a[i] = malloc((1024+1) * sizeof(char)); 
    int i = 0; 
    while (fscanf(f, " %1023s", x) == 1) { 
     strcpy(a[i], x); 
     i++; 
    } 
    for (int j = 0; j < i; j++) { 
     printf("%d %s\n", j, a[j]); 
    } 
} 
int main(void){ 
    read_words(fopen("1.txt", "r")); 
    return 0; 
} 

測試(這裏是你的陣列)

$ ./a.out 
0 Sed 
1 ut 
2 perspiciatis 
3 unde 
4 omnis 
5 iste 
6 natus 
7 error 
8 sit 
9 voluptatem 
10 accusantium 
11 doloremque 
12 laudantium, 
13 totam 
14 rem 
15 aperiam, 
16 eaque 
17 ipsa 
18 quae 
19 ab 
20 illo 
21 inventore 
22 veritatis 
23 et 
24 quasi 
25 architecto 
26 beatae 
27 vitae 
28 dicta 
29 sunt 
30 explicabo. 
31 Nemo 
32 enim 
33 ipsam 
34 voluptatem 
35 quia 
36 voluptas 
37 sit... 
+0

此seg故障。 – Mmsyther

+0

這是必須工作的測試文件。 – Mmsyther

+0

Loomings 給我打電話Ishmael。幾年前,不管什麼時候,只要我的錢包裏沒有什麼錢,也沒有什麼特別讓我感興趣的話,我想我會航行一小會兒,看看世界上那個水汪汪的地方。這是我驅除脾氣和調節血液循環的一種方式。每當我發現自己在口中變得嚴峻時,每當它潮溼,十一月在我的靈魂中,每當我發現mys – Mmsyther