gpt4 book ai didi

c - 使用 strtok 存储数组

转载 作者:行者123 更新时间:2023-11-30 17:23:20 25 4
gpt4 key购买 nike

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 行。我希望每一行都存储在一个数组中。我怎样才能做到这一点 ?这始终存储第一个元素。

我的输入文件:

George    :Math1,History2,Math2
ELizabeth :Math2,Germany1,spanish1
Adam :Germany1,History2,Math1

最佳答案

为了将这三行读入数组,为什么不使用像这样简单的东西:

 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++;
}

关于c - 使用 strtok 存储数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27649985/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com