将字符串存储到C中的链表中



在我的问题中,我需要从txt文件中读取行
每行包含student_id、姓名、出生日期、性别、部门和年级(班级(
例如(155898933;Nuh Kaplan;1998年1月26日;M;ME;2(
然后我需要将它们存储在链表中
我声明我的结构如下:

struct Node{
int id;
char name[100];
int date; //YYYYMMDD (String date will be converted to integer. I created dateStrInt function for this conversion.)
char gender[2];
char departmentCode[6];
int classGrade;
struct Node *next;
};

然后我读取行并将其存储在一个字符数组中,其中包含以下代码部分:

if (fptr != NULL) {
while (fgets (buff, 100, fptr)) {
char* split = strtok(buff, ";");
while( split != NULL ) {
strcpy(arr[i][j], split); 
j++;
split = strtok(NULL, ";");
}
i++;
j = 0;
} 
}

在我将行存储到字符数组中之后,我需要将它们存储在链表中。我试着这样做:

for(int i = 0; i < len; i++) {
p = (struct Node*) malloc(sizeof(struct Node));
j = 0;
p->id = (int)arr[i][j];
strcpy(p->name, arr[i][j+1]);
p->date = dateStrInt(arr[i][j+2]);
strcpy(p->gender,arr[i][j+3]);
strcpy(p->departmentCode, arr[i][j+4]);
p->classGrade = (int)arr[i][j+5];

j = 0;
if(list == NULL){
list = p;
p -> next = NULL;
last = p;
}
else {
last -> next = p;
p -> next = NULL;
last = p;
}
}

然而,当我运行代码时,我会得到studentid和classGrade的垃圾值。你能帮我解决这个问题吗?

我的孔代码:

#include <stdio.h>
#include<string.h>
#include <stdlib.h>
#include <ctype.h>
struct Node {
int id;
char name[100];
int date; //YYYYMMDD
char gender[2];
char departmentCode[6];
int classGrade;
struct Node *next;
};
int dateStrInt(char str[]) {
int d, m, y;
sscanf(str, "%d/%d/%d", &d, &m, &y);
return (y * 10000 + m * 100 + d);
} 
int countLines(char* filename) {
int count = 0;
FILE *fptr;
fptr = fopen(filename, "r");
if(fptr == NULL){
printf("File does not exist.");
exit(1);
}
else {
for(char ch = getc(fptr); ch != EOF; ch = getc(fptr)) {
if(ch == 'n'){
count++;
}
}
}
return count + 1;
}
struct Node* createList(struct Node* list, int len, char filename[]){
struct Node* last;
struct Node* p;
char buff[100];
char arr[len][6][100];
int i = 0, j = 0;
FILE* fptr = fopen(filename,"r");

if (fptr != NULL) {
while (fgets (buff, 100, fptr)) {
char* split = strtok(buff, ";");
while( split != NULL ) {
strcpy(arr[i][j], split); 
j++;
split = strtok(NULL, ";");
}
i++;
j = 0;
} 
}

int o = 0;
for(int k = 0; k < len; k++){
printf("nID = %s", arr[k][o]);
printf("nName = %s", arr[k][o+1]);
printf("nBirth Date = %s", arr[k][o+2]);
printf("nGender = %s", arr[k][o+3]);
printf("nDepartment = %s", arr[k][o+4]);
printf("nGrade = %s", arr[k][o+5]);
o = 0;
}


for(int i = 0; i < len; i++){
p = (struct Node*) malloc(sizeof(struct Node));
j = 0;
p->id = (int)arr[i][j];
strcpy(p->name, arr[i][j+1]);
p->date = dateStrInt(arr[i][j+2]);
strcpy(p->gender,arr[i][j+3]);
strcpy(p->departmentCode, arr[i][j+4]);
p->classGrade = (int)arr[i][j+5];

j = 0;
if(list == NULL){
list = p;
p -> next = NULL;
last = p;
}
else {
last -> next = p;
p -> next = NULL;
last = p;
}
}  
return list;
}
void printList(struct Node *list, int len){
struct Node *p;
p = list;
int cnt = 0;
printf("nThe list = ");
while(p != NULL) {
if(cnt != len - 1) {
printf("n{%d, %s, %d, %s, %s, %d} ---->",p->id, p->name, p->date,
p->gender, p->departmentCode, p->classGrade);
cnt++;
}
else
printf("n{%d, %s, %d, %s, %s, %d} ",p->id, p->name, p->date,
p->gender, p->departmentCode, p->classGrade);
p = p->next;
}
}
int main(void) {
struct Node *head = NULL;
char filename[10];
printf("Enter file name: ");
scanf("%s", filename);
int len = countLines(filename);
struct Node* list = createList(head, len, filename);
printList(list, len);
}

test.txt文件:

149875280;布尔库阿克苏;1994年4月4日;FCS;3

180201201;Mustafa Kursat Yavuz Tur;1996年6月12日;MCS;3

输出:

输入文件名:test.txt
ID=149875280
姓名=Burcu Aksu
出生日期=1994年4月4日
性别=F
部门=CS
等级=3

ID=180201201
名称=Mustafa Kursat Yavuz Tur
生日=1996年6月12日
性别=M
部门=CS
等级=3

列表=
{-18835589280,布尔库阿克苏,19940404,F,CS,-1883588780}---->
{-1883588680,Mustafa Kursat Yavuz Tur,19960612,M,CS,-1883588180}

为什么要声明char数组,而应该创建Node类型的数组。仅将字符串值类型转换为int将无法正确转换这两个整数字段。使用类似atoi()的转换函数。更改此行,

p->id = (int)arr[i][j];

p->id = atoi(arr[i][j]);

相关内容

  • 没有找到相关文章

最新更新