Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将strcpy与链表成员一起使用时出现分段错误_C_Linked List_Segmentation Fault_Strcpy - Fatal编程技术网

将strcpy与链表成员一起使用时出现分段错误

将strcpy与链表成员一起使用时出现分段错误,c,linked-list,segmentation-fault,strcpy,C,Linked List,Segmentation Fault,Strcpy,我有一个包含以下数据的文本文件: 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 cou

我有一个包含以下数据的文本文件:

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] != '\0'){
        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] = '\0';
            }
            printf("%s\n", stdID);
            strcpy(loadStudent ->studentID, stdID);    //Causing segmentation fault
            commaCount++;
        }
struct-courseInfo{
int courseID;
char-courseName[30];
};
typedef结构courseInfo cInf;
结构学生信息{
char-studentID[8];
charfirstn[20];
char-lastN[25];
国际课程;
cInf courseInf[10];
结构studentInfo*下一步;
};
类型定义结构学生信息sInf;
void loadStudentInfo(){
文件*加载文件;
loadFile=fopen(“studentRecords.txt”、“r”);
而(1){
sInf*学生;
loadStudent=(sInf*)malloc(sizeof(sInf));
字符温度[256];
fgets(临时,256,加载文件);
如果(临时[0]='*')
打破
int commaCount=0;
int i=0;
而(临时[i]!='\0'){
如果(临时[i]==','&&commaCount==0){
char-stdID[8];
strcpy(stdID,Temp);
int commaLoc=0;
while(stdID[commaLoc]!=','){//查找stdID的结尾
commaLoc++;
}
对于(;commaLoc<8;commaLoc++){//分隔stdID,8是studentID的最大大小
stdID[commaLoc]='\0';
}
printf(“%s\n”,stdID);
strcpy(loadStudent->studentID,stdID);//导致分段错误
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;
}

我想你遗漏了一个
typedef struct studentInfo sInf;
而且,这个程序不能出错。它处于一个无限循环中,因为你的
while
条件总是正确的,但是它里面的
if
的条件总是错误的。提示:
I
从不递增。是的,正如我前面所说的,我只把这一部分放在at给了我seg错误。我让我在下面递增,但我没有复制它。假设没有无限循环(因为没有),并且我正在递增(因为它是)。不是
strcpy(stdID,Temp);
要复制约35个字符(即,文件的整个第一行)通过8个字符宽的缓冲区
stdId
?这可能足以损坏堆栈上的其他变量,并给您带来随机行为。我让strtok在第一行工作,但在循环到第二行时它会发生故障。我现在将尝试扩展stdId的大小,我会回复您。谢谢,我仍然获得了se在扩展设备的大小时出现分段错误stdID@user7438591您是如何更改stdID的?新的大小?是的,刚将大小更改为256M,我还尝试再次使用strtok(因为我以前保存了代码)但这一次,所有strtok调用都在一个不同的函数中,该函数随后被上述函数调用,这也导致了seg故障
11111
First
Last
2
COR
100
SEG
200