2013-05-04 95 views
-3

剛剛有一個問題,當您從文本文件中讀取文本行時,如何分隔這些文本並將它們存儲到數組中。將文本文件讀入C中的數組

例如,如果我有兩行文字在我的文本文件,它看起來像這樣:

1005; AndyCool;安迪;安德森; 23; LA 1006; JohnCool;約翰;安德森; 23; LA

如何將它們拆分爲基於';' 。 然後將它們存儲在二維數組中。

對不起,我還沒開始我的編碼,只是還沒有到這裏貼

乾杯......

+0

檢查了這一點: http://stackoverflow.com/questions/2523467/how-to-split-a-string-to-2-strings-in -c – SatA 2013-05-04 07:53:01

回答

1

使用strsep功能:

char* token; 
char* line; 

/* I assume the line as loaded from file */; 

if(line != NULL) { 
    while ((token = strsep(&line, ";")) != NULL) 
    { 
    /* 
     token points to the current extracted string, 
     use it to fill your array 
     */ 
    } 

} 
+0

謝謝...我會檢查出來... – LearningHowtoCode 2013-05-04 08:02:07

0

看那fopen,fgets,strstr和strchr和strspn函數的手冊頁... strtok和strsep函數也適用於大多數您將要執行的操作。