使用strtok存储数组 [英] Store an array using strtok
本文介绍了使用strtok存储数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
int main (){
FILE *file = fopen ( "C:\\input.txt", "r" );
int i=0, j=0, k=0;
char *result[10][10];
char line[100];
char *value;
char *res[100][100];
for(i=0; i<=9; i++){
for(j=0;j<=9;j++){
result[i][j] = NULL;
}
}
while(fgets(line, sizeof(line), file)){
char *array=strtok(line,"\n");
res[0][0]=strdup(array);
printf("\n\n\n %s RES \n",res[0][0]);
array=strtok(array,"\n");
res[0][1]=strdup(array);
printf("\n\n\n %s RES \n",res[0][1]);
array=strtok(line,"\n");
res[0][2]=strdup(array);
}
我想逐行将数组存储在txt文件中。我的输入文件中有 3
行。我希望每一行都存储在一个数组中。我怎样才能做到这一点 ?
I want to store an array in a txt file line by line. There are 3
rows in my input file. I want every line is stored by in an array. How can I do that ? this is always store first element.
我的输入文件:
George :Math1,History2,Math2
ELizabeth :Math2,Germany1,spanish1
Adam :Germany1,History2,Math1
推荐答案
为将这三行读入数组,为什么不使用像这样简单的东西:
For reading those three lines into array why don't you use something as simple as this:
char res[100][100];
int i =0;
while(fgets(line, sizeof(line), file)){
strcpy(&res[i][0],line);
printf("%s \n",&res[i][0]);
i++;
}
这篇关于使用strtok存储数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文