将 strcpy 与链表成员一起使用时出现分段错误
Segmentation Fault when using strcpy with a linked list member
我有一个包含以下数据的文本文件:
11111,First,Last,2,COR,100,SEG,200
22222,F,L,1,COR,100
33333,Name,Name,3,COR,100,SEG,200,CMP,300
***
我需要将每一行的数据(逗号处)分开,并将每一行数据添加到链表中。这是我用来执行此操作的一段代码:
struct courseInfo{
int courseID;
char courseName[30];
};
typedef struct courseInfo cInf;
struct studentInfo{
char studentID[8];
char firstN[20];
char lastN[25];
int nCourses;
cInf courseInf[10];
struct studentInfo *next;
};
typedef struct studentInfo sInf;
void loadStudentInfo(){
FILE *loadFile;
loadFile = fopen("studentRecords.txt", "r");
while(1){
sInf *loadStudent;
loadStudent = (sInf*)malloc(sizeof(sInf));
char Temp[256];
fgets(Temp,256,loadFile);
if (Temp[0] == '*')
break;
int commaCount = 0;
int i = 0;
while(Temp[i] != '[=11=]'){
if(Temp[i] == ',' && commaCount == 0){
char stdID[8];
strcpy(stdID, Temp);
int commaLoc = 0;
while(stdID[commaLoc] != ','){ //Finds where stdID ends
commaLoc++;
}
for(;commaLoc < 8; commaLoc++){ //Delimits stdID, 8 is the max size of studentID
stdID[commaLoc] = '[=11=]';
}
printf("%s\n", stdID);
strcpy(loadStudent ->studentID, stdID); //Causing segmentation fault
commaCount++;
}
现在,我为分隔每一行中的数据所做的工作可能不是最有效的,但我尝试使用 strtok() (它可以很好地读取第一行但在读取第二行时导致分段错误)并尝试使用 fscanf 和 %[^,] 但它对我不起作用。无论如何,我不认为我用来分离数据的方法会导致分段错误。此循环将继续分离其余数据并将其存储在 loadStudent 的适当成员中。导致分段错误的行如上所示。感谢您帮助确定此错误的原因。
您的问题从这里开始:
strcpy(stdID, Temp);
这会将 整个 第一行复制到 stID
中。由于 stID
只能容纳 8 个字符,而第一行超过 8 个字符,因此您写出越界,这是未定义的行为。之后什么都可能发生。
您写道您无法 strtok
工作。试试这个:
#include <stdio.h>
#include <string.h>
int main(void) {
char str[] = "11111,First,Last,2,COR,100,SEG,200";
char* p = strtok(str, ",");
while(p)
{
printf("%s\n", p);
p = strtok(NULL, ",");
}
return 0;
}
输出:
11111
First
Last
2
COR
100
SEG
200
我有一个包含以下数据的文本文件:
11111,First,Last,2,COR,100,SEG,200
22222,F,L,1,COR,100
33333,Name,Name,3,COR,100,SEG,200,CMP,300
***
我需要将每一行的数据(逗号处)分开,并将每一行数据添加到链表中。这是我用来执行此操作的一段代码:
struct courseInfo{
int courseID;
char courseName[30];
};
typedef struct courseInfo cInf;
struct studentInfo{
char studentID[8];
char firstN[20];
char lastN[25];
int nCourses;
cInf courseInf[10];
struct studentInfo *next;
};
typedef struct studentInfo sInf;
void loadStudentInfo(){
FILE *loadFile;
loadFile = fopen("studentRecords.txt", "r");
while(1){
sInf *loadStudent;
loadStudent = (sInf*)malloc(sizeof(sInf));
char Temp[256];
fgets(Temp,256,loadFile);
if (Temp[0] == '*')
break;
int commaCount = 0;
int i = 0;
while(Temp[i] != '[=11=]'){
if(Temp[i] == ',' && commaCount == 0){
char stdID[8];
strcpy(stdID, Temp);
int commaLoc = 0;
while(stdID[commaLoc] != ','){ //Finds where stdID ends
commaLoc++;
}
for(;commaLoc < 8; commaLoc++){ //Delimits stdID, 8 is the max size of studentID
stdID[commaLoc] = '[=11=]';
}
printf("%s\n", stdID);
strcpy(loadStudent ->studentID, stdID); //Causing segmentation fault
commaCount++;
}
现在,我为分隔每一行中的数据所做的工作可能不是最有效的,但我尝试使用 strtok() (它可以很好地读取第一行但在读取第二行时导致分段错误)并尝试使用 fscanf 和 %[^,] 但它对我不起作用。无论如何,我不认为我用来分离数据的方法会导致分段错误。此循环将继续分离其余数据并将其存储在 loadStudent 的适当成员中。导致分段错误的行如上所示。感谢您帮助确定此错误的原因。
您的问题从这里开始:
strcpy(stdID, Temp);
这会将 整个 第一行复制到 stID
中。由于 stID
只能容纳 8 个字符,而第一行超过 8 个字符,因此您写出越界,这是未定义的行为。之后什么都可能发生。
您写道您无法 strtok
工作。试试这个:
#include <stdio.h>
#include <string.h>
int main(void) {
char str[] = "11111,First,Last,2,COR,100,SEG,200";
char* p = strtok(str, ",");
while(p)
{
printf("%s\n", p);
p = strtok(NULL, ",");
}
return 0;
}
输出:
11111
First
Last
2
COR
100
SEG
200