2013-05-04 94 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函数也适用于大多数您将要执行的操作。