gpt4 book ai didi

c - 使用 fgets() 和 strtok() 在 C 中逐行读取文件?

转载 作者:行者123 更新时间:2023-12-02 06:20:08 24 4
gpt4 key购买 nike

我正在尝试使用 fgets 和 strtok() 逐行读取文件,并创建每个不同信息行的链接列表。

现在,我只是将信息放入一个数组中,只是想弄清楚如何正确读取信息,但它无法正常工作。

在 while(fgets) 部分,它似乎将所有内容正确加载到数组中,并将其打印出来。然而,在该循环执行之后,我尝试打印出整个数组,我得到了非常奇怪的结果......这主要是最后一行的大部分内容,而不是完整的单词或大部分内容。

例如,如果我正在阅读:

Simpson, Homer, Male, 1976
Simpson, Marge, Female, 1978
Simpson, Bart, Male, 2002
Simpson, Lisa, Female, 2004
Simpson, Maggie, Female, 2011

我最后得到的打印输出是这样的:
le
Simpson
Maggie


Simpson
Maggie
e
ale
Simpson
Maggie
e
e
Simpson
Maggie
Female
2011

请告诉我哪里出错了,谢谢!
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXSTRINGSIZE 10
#define LINESIZE 128

struct person{
char firstName[MAXSTRINGSIZE];
char lastName[MAXSTRINGSIZE];
char gender[MAXSTRINGSIZE];
int birthYear;
struct person *next;
} *first, *current;


int main (void){
static const char filename[] = "Assignment1file.txt";
FILE *myfile = fopen ( "Assignment1file.txt", "r" );

int i=0;
int j=0;
int k=0;
int l=0;
char *result[10][4];
char line[LINESIZE];
char *value;

for(i=0; i<9; i++){
for(j=0;j<4;j++){
result[i][j] = NULL;
}
}
i=0;

// loop through each entry in Assignment1file
while(fgets(line, sizeof(line), myfile)){

//load last name
value = strtok(line, ",");
result[i][0] = value;
printf("%i 0 %s", i, value);


//load first time
value = strtok(NULL, ",");
result[i][1] = value;
printf("%i 1 %s", i, value);

// load gender
value = strtok(NULL, ",");
result[i][2] = value;
printf("%i 2 %s", i, value);

// load birth year
value = strtok(NULL, "\n");
result[i][3] = value;
printf("%i 3 %s", i, value);
printf("\n");

for(j=0;j<4;j++){
printf("%s\n", result[i][j]);
}


//go to next line
i++;
}

// read out the array
for(k=0; k<5; k++){
for(j=0;j<4;j++){
printf("%s\n", result[k][j]);
}
}

fclose(myfile);
return 0;
}

最佳答案

这段代码有几个问题。我已经快速修改了您的代码以执行预期的操作。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXSTRINGSIZE 10
#define LINESIZE 128

struct person{
char firstName[MAXSTRINGSIZE];
char lastName[MAXSTRINGSIZE];
char gender[MAXSTRINGSIZE];
int birthYear;
struct person *next;
} *first, *current;


int main (void){
FILE *myfile = fopen ( "Assignment1file.txt", "r" );
int i=0;
int j=0;
int k=0;
int l=0;
char *result[10][4];
char line[LINESIZE];
char *value;

for(i=0; i<=9; i++){
for(j=0;j<=4;j++){
result[i][j] = NULL;
}
}
i=0;

// loop through each entry in Assignment1file
while(fgets(line, sizeof(line), myfile)){
//load last name
value = strtok(line, ", ");
result[i][0] = strdup(value);
printf("last: %s\n", value);


//load first time
value = strtok(NULL, ", ");
result[i][1] = strdup(value);
printf("first: %s\n", value);

// load gender
value = strtok(NULL, ", ");
result[i][2] = strdup(value);
printf("gender: %s\n", value);

// load birth year
value = strtok(NULL, " \n");
result[i][3] = strdup(value);
printf("birth year: %s\n", value);

//go to next line
i++;
}

// read out the array
for(k=0; k<5; k++){
for(j=0;j<4;j++){
printf("%s\n", result[k][j]);
}
}

fclose(myfile);
return 0;
}

人们已经对这一变化的细节发表了评论。

关于c - 使用 fgets() 和 strtok() 在 C 中逐行读取文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12499219/

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