2017-01-21 8 views
-1
int main() 
{ 
    char *temp_list[4096]; 
    char *list[4096]; 
    char *token; 
    int countries; 
    ssize_t rd_countries; 
    char buf[512]; 
    size_t nbytes; 
    char *file_name = "AllCountries.dat"; 
    int temp = 0; 
    int i = 0; 
    int j = 0; 

    countries = open(file_name,O_RDONLY); 
    nbytes = sizeof(buf); 

    // start of do while to read through the list and set tokens to 
    // the array list the file is separated by commas 
    //and have to use the open function (nothing else) 

    do {             
    rd_countries = read(countries, buf, nbytes);  
    token = strtok_r(buf, ",",temp_list); 
     while (token != NULL){     
      temp_list[i] = token; 
      printf("%s\n ||||||||||| ", temp_list[i]); 
     printf("%s\n", token); 
     token = strtok_r(NULL, ",",temp_list); 
     i = i + 1; 
     }         
    printf("-----------"); 
    }while (rd_countries != 0); 

    //here is where i see whats in the temp list but everything is wrong 

    for (j = 0; j < 100; j++){ 
    printf("The temp list: %s\n", temp_list[j]); 
    } 
// for(int k = 0; k < 4096; k++){ 
// if (k == 1 || k == 2 || k == 8 || k == 9) { 
// list[k] = temp_list[k]; 
//  } 
// } 

//239 countries biggest country is 44 char long 
return 0; 

} 

がありながら行った後、間違った出力を返していません:バッファがないポインタの私の配列を保存し、出力の一部、ここでループ

##outputs 
    ||||||||||| URY 
Uruguay 
    ||||||||||| Uruguay 
South America 
    ||||||||||| South America 
South America 
    ||||||||||| South America 
175016 
    ||||||||||| 175016 
1828 
    ||||||||||| 1828 
3337000 
    ||||||||||| 3337000 
75.2 
    ||||||||||| 75.2 
20831 
    ||||||||||| 20831 
Uruguay 
    ||||||||||| Uruguay 
Republic 
    ||||||||||| Republic 
Jorge Batlle Ib��ez 
    ||||||||||| Jorge Batlle Ib��ez 
3492 
    ||||||||||| 3492 
UY 
23 
    ||||||||||| UY 
23 
VEN 
    ||||||||||| VEN 
Venezuela 
    ||||||||||| Venezuela 
South America 
    ||||||||||| South America 
South America 
    ||||||||||| South America 
912050 
    ||||||||||| 912050 
1811 
    ||||||||||| 1811 
24170000 
    ||||||||||| 24170000 
73.1 
    ||||||||||| 73.1 
95023 
    ||||||||||| 95023 
Venezuela 
    ||||||||||| Venezuela 
Federal Republic 
    ||||||||||| Federal Republic 
Hugo Ch�vez Fr�as 
    ||||||||||| Hugo Ch�vez Fr�as 
3539 
    ||||||||||| 3539 
VE 
nz�lez Macchi 
    ||||||||||| VE 
nz�lez Macchi 
-----------a Corazao 
    ||||||||||| a Corazao 
-----------The temp list: 
The temp list: a Corazao 
The temp list: a Corazao 
The temp list: azao 
The temp list: o 
The temp list: 0 
The temp list: PE 
152 
The temp list: 2 
The temp list: SUR 
The temp list: h America 
The temp list: 1.4 
The temp list: ame 
The temp list: epublic 
The temp list: c 
The temp list: nald Venetiaan 
The temp list: n 
The temp list: SR 
31 
The temp list: ay 
The temp list: h America 
The temp list: rica 
The temp list: h America 
The temp list: rica 
The temp list: 175016 
The temp list: 337000 
The temp list: .2 
The temp list: 
The temp list: ublic 
The temp list: orge Batlle Ib��ez 
The temp list: Batlle Ib��ez 
The temp list: Ib��ez 
The temp list: 3492 
The temp list: Venezuela 
The temp list: ela 
The temp list: outh America 
The temp list: rica 
The temp list: South America 
The temp list: h America 
The temp list: 1811 
The temp list: 0000 
The temp list: 3 
The temp list: nezuela 
The temp list: a 
The temp list: deral Republic 
The temp list: ublic 
The temp list: ugo Ch�vez Fr�as 
The temp list: s 
The temp list: VE 
nz�lez Macchi 
The temp list: �lez Macchi 
The temp list: hi 
The temp list: 85 
The temp list: 
27 
The temp list: America 
The temp list: South America 
The temp list: ica 
The temp list: 285216 
The temp list: 1821 
The temp list: 
The temp list: 0 
The temp list: 
The temp list: ublic 
The temp list: 
The temp list: ntin Paniagu����� 
The temp list: niagu����� 
The temp list: ����� 
The temp list: a Corazao 
The temp list: razao 
The temp list: PE 
152 
The temp list: R 
The temp list: h America 
The temp list: rica 
The temp list: 63265 
The temp list: 5 
The temp list: riname 
The temp list: Republic 
The temp list: Venetiaan 
The temp list: aan 
The temp list: 243 
The temp list: 1 
The temp list: RY 
The temp list: uguay 
The temp list: th America 
The temp list: a 
The temp list: 00 
The temp list: .2 
The temp list: 31 
The temp list: ruguay 
The temp list: y 
The temp list: lic 
The temp list: e Ib��ez 
The temp list: 
The temp list: 
The temp list: EN 
The temp list: nezuela 
The temp list: la 
The temp list: rica 
The temp list: h America 
The temp list: 2050 
The temp list: 1811 
The temp list: 170000 
The temp list: 00 
+0

ファイルをどうにかしてください。 –

+0

その長いファイルがほしいと思えばそれを掲示できる – jhowe

+0

115、DZA、アルジェリア、アフリカ、北アフリカ、2381741,1962,31471000,69.7,49982、Al-Jazair /Algérie、Abdelaziz Bouteflika、35、DZアフリカ、西アフリカ、112622,1960、ベノア、アフリカ、アフリカ、アフリカ、アフリカ、1246700,1975,12878000,38.3,6648、アンゴラ、共和国、ジョゼ・エドゥアルド・ドス・サントス、56、AO 94、 6097000,50.2,2357、Bénin、Republic、MathieuKérékou、187、BJ – jhowe

答えて

1

あなたが読み取りを行うと()ファイルから必然的に\ 0で終了します。 strtokがbufを処理するためには、bufの内容にヌル終了文字列を設定する必要があります。

など。 Iharobが指摘したように、あなたがこれを実行して、その上にはstrtokを実行した後、あなたが次に

rd_countries = read(countries, buf, nbytes-1); 
if (rd_countries>-1) 
{ 
    buf[rd_countries] = '\0'; 
} 

ような何かを行うことができますstrtokは、それにそうするたびにそれを渡された文字列を変更しますので、あなたは、メモリを割り当てるとtokenをコピーする必要がありますポインタを返します。前のポインタは無効です。また、あなたがstrtok_rに渡している文字列をNULLで終端しないことに加えて

temp_list[i] = strdup(token); 
+0

区切り文字を ''\ 0' 'に置き換え、それは観測された振る舞いを引き起こさないでしょう。 –

+0

OPとして使用されている場合、temp_listポインタは有効ではなく、その動作を説明することがあります。 strtokと一緒に使用すると、bufも0で終了する必要があります。 –

+0

はい、私は下のコードを参照してください、あなたの答えは正しいです。しかし、OPは文字列をコピーするために 'strdup()'が 'malloc()'を使ってバッファを割り当てることをOPが確かに知っていないので、 'free()'と言いましょう。私の答えで説明されていることのために、未定義の動作が起こることを指摘してください。 –

1

のように書くことができ

temp_list[i] = malloc(strlen(token)+1); 
strcpy(temp_list[i], token); 

、他のいくつかの問題がある:

  • 第三は、 strtok_rの引数はコンテキストポインタです。 は何もしないでください(設定すると、 の値などがあることが予想されます)。 char *ctxtと宣言し、 を&ctxtとしてください(詳細はman pageを参照してください)。
  • ファイルから読み取ると、一度に512バイトを読み取っていますが、 何も読み取られていないように見えません。何らかの値の真ん中で が終了しています 次回)。私はあなたのデータがどのように見えるかはわかりませんが、 はおそらく別の方法でそれを読んでいるはずです。
+0

はい、ちょうどそれをどのように取り組むためにどのようなアイデアを考え出したのですか? @Andy Schweig – jhowe

+0

データの形式は何ですか?それが行なら、 'fgets'を使って一度に行を読むことができます。また、ファイルが大きすぎない場合は、適切なサイズのバッファを 'malloc'して、一度にすべてを読むことができます。 –

+0

よく私は最後のカンマの後にデータを消去し、その後にポインタを設定することを考えていましたが、もし可能ならばどうしたらよいか分かりません(データに新しい行があり、区切られていますコンマで区切って) – jhowe

関連する問題