我正在尝试创建一个链表来存储来自文本文件的输入。 我可以获得第一组信息,但是当我获得后续信息时,出现段错误。
请告诉我我做错了什么。
这是我的全部代码
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int CourseID;
char CourseName[30];
} courseInfo;
struct StudentInfo{
char StudentID[9];
char FirstName[20];
char LastName[25];
int Courses;
courseInfo CourseInfo[10];
struct StudentInfo *next;
};
typedef struct StudentInfo studentInfo;
typedef studentInfo *ListPtr;
studentInfo LoadFile(studentInfo *RootPtr);
void PrintStruct(studentInfo *startPtr);
int main()
{
studentInfo studentList[100];
ListPtr HeadPtr = NULL;
printf("Loading File...\n");
LoadFile(HeadPtr);
printf("Load Success\n");
PrintStruct(studentList);
printf("AfterPrint\n");
return 0;
}
studentInfo LoadFile( studentInfo *RootPtr)
{
FILE * ptrFile;
ListPtr NodePtr = malloc(sizeof (studentInfo));
ListPtr curr;
int counter, i=0;
ptrFile = fopen("studentInfo.txt", "r");
if( ptrFile == NULL)
printf("Open Unsuccessful\n");
fscanf(ptrFile,"%s", NodePtr->StudentID);
printf("%s\n", NodePtr->StudentID);
fscanf(ptrFile,"%s", NodePtr->FirstName);
printf("%s\n", NodePtr->FirstName);
fscanf(ptrFile,"%s", NodePtr->LastName);
printf("%s\n", NodePtr->LastName);
fscanf(ptrFile,"%d", &(NodePtr->Courses));
printf("%d\n", NodePtr->Courses);
for(counter = 0; counter <= NodePtr->Courses; counter++)
{
fscanf(ptrFile,"%s", NodePtr->CourseInfo[counter].CourseName);
printf("%s ", NodePtr->CourseInfo[counter].CourseName);
fscanf(ptrFile,"%d", &(NodePtr->CourseInfo[counter].CourseID));
printf("%d\n", NodePtr->CourseInfo[counter].CourseID);
}
curr = RootPtr;
NodePtr->next = NULL;
RootPtr = NodePtr;
NodePtr = NodePtr -> next;
while( strcmp("***", NodePtr->StudentID) !=0 )
{
fscanf(ptrFile,"%s", NodePtr->StudentID);
printf("%s\n", NodePtr->StudentID);
fscanf(ptrFile,"%s", NodePtr->FirstName);
printf("%s\n", NodePtr->FirstName);
fscanf(ptrFile,"%s", NodePtr->LastName);
printf("%s\n", NodePtr->LastName);
fscanf(ptrFile,"%d", &(NodePtr->Courses));
printf("%d\n", NodePtr->Courses);
for(counter = 0; counter <= NodePtr->Courses; counter++)
{
fscanf(ptrFile,"%s", NodePtr->CourseInfo[counter].CourseName);
printf("%s ", NodePtr->CourseInfo[counter].CourseName);
fscanf(ptrFile,"%d",&(NodePtr->CourseInfo[counter].CourseID));
printf("%d\n", NodePtr->CourseInfo[counter].CourseID);
}
NodePtr = NodePtr->next;
}
fclose(ptrFile);
return *NodePtr;
}
void PrintStruct(studentInfo *startPtr)
{
int i;
ListPtr begin;
printf("In print\n");
for( begin = startPtr; begin->next != NULL; begin=begin->next)
{
printf("%s\n", begin->StudentID);
printf("%s\n", begin->FirstName);
printf("%s\n", begin->LastName);
printf("%d\n", begin->Courses);
for(i = 0; i <= begin->Courses; i++)
{
printf("%s", begin->CourseInfo[i].CourseName);
printf("%d\n", begin->CourseInfo[i].CourseID);
}
}
return;
}
`
我的文本文件是: 111111111 丽莎 搬运工 3个 电气工程 114 中国移动412 恩美 515 333333333 亚历克斯 辛普森 1个 中国移动412
并且最多只会打印“333333333” 后跟一个“0”,然后是一个段错误。
最佳答案
对字符串输入扫描到不足的缓冲区没有限制。
以下尝试将“111111111”存储到 char
数组中。第一个是 9 个 char
,其中 fscanf(ptrFile,"%s",...)
扫描并存储到 NodePtr->StudentID
-一个 9 char
数组。然后 fscanf
附加一个 '\0'
并在调用未定义行为 (UB) 的数组外写入。
struct StudentInfo{
char StudentID[9];
...
// My text file is : 111111111 Lisa Porter 3 ENEE 114 CMSC 4
fscanf(ptrFile,"%s", NodePtr->StudentID);
代码应该 1) 分配足够的空间 2) 使用 "%9s"
限制输入长度和 3) 检查 fscanf()
结果。更好:
struct StudentInfo{
// Enough space for 9 char and \0
char StudentID[9+1];
...
// add if and ------------v
if (1 != fscanf(ptrFile,"%9s", NodePtr->StudentID))
Handle_Error();
关于c - 链接列表和 fscanf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29453087/