C 加载文件时进行内核转储
我试图创建一个从.txt文件加载数据的函数,但当它运行时,我总是得到一个分段错误(内核转储)错误。该文件包含未知数量的行,而每行都有一个字符串和一个由制表符分隔的整数。list_create函数只创建一个数据结构。最后的while循环删除了数据结构,我没有包含代码,因为我确信这不会导致问题,但我也想表明我正在释放数据结构。值得一提的是,当使用gdb时,我得到:C 加载文件时进行内核转储,c,segmentation-fault,coredump,C,Segmentation Fault,Coredump,我试图创建一个从.txt文件加载数据的函数,但当它运行时,我总是得到一个分段错误(内核转储)错误。该文件包含未知数量的行,而每行都有一个字符串和一个由制表符分隔的整数。list_create函数只创建一个数据结构。最后的while循环删除了数据结构,我没有包含代码,因为我确信这不会导致问题,但我也想表明我正在释放数据结构。值得一提的是,当使用gdb时,我得到: Program received signal SIGSEGV, Segmentation fault. 0x0000555555554
Program received signal SIGSEGV, Segmentation fault.
0x0000555555554c46 in load (filename=0x7fffffffe2ab "students.txt",
l=0x555555757260) at Student.c:92
92 tmp->next=malloc(sizeof(struct _node));
我试着用其他东西来改变feof,使用ferror和不使用ferror,并将fopen的模式改为r而不是a
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#define MAXSTRING 50
typedef struct{
char name[MAXSTRING];
int id;
} student;
typedef struct _node* node;
typedef struct _list* list;
struct _node {
student data;
node next;
};
struct _list {
node head;
int size;
};
list list_create(){
list l=(list) malloc(sizeof(struct _list));
assert(1);
l->head=NULL;
l->size=0;
return l;
}
void load(char*filename,list l){
FILE *fd=fopen(filename,"r");
node tmp=l->head;
if(fd==NULL){
printf("Error trying to open the file\n");
abort();
}
else{
while(!feof(fd)&&!ferror(fd)){
fscanf(fd,"%s\t%d\n",tmp->data.name,&tmp->data.id);
tmp->next=(node)malloc(sizeof(struct _node));
assert(tmp->next);
tmp=tmp->next;
l->size++;
if (tmp==NULL){
printf("Error trying to allocate memory\n");
abort();
}
}
}
tmp->next=NULL;
fclose(fd);
}
int main(int argc,char *argv[]){
list l=list_create();
if(argc!=2){
printf("Input Error\n");
}
load(argv[1],l);
\*Some code*\
while (!list_empty(l)){
list_freenode(list_deletefirst(l));
}
free(l);
return 0;
#包括
#包括
#包括
#包括
#定义MAXSTRING 50
类型定义结构{
字符名[MAXSTRING];
int-id;
}学生;
typedef结构_节点*节点;
类型定义结构列表*列表;
结构节点{
学生资料;
节点下一步;
};
结构列表{
节点头;
整数大小;
};
列表\u创建(){
list l=(list)malloc(sizeof(struct_list));
断言(1);
l->head=NULL;
l->size=0;
返回l;
}
无效加载(字符*文件名,列表l){
FILE*fd=fopen(文件名,“r”);
节点tmp=l->head;
如果(fd==NULL){
printf(“尝试打开文件时出错\n”);
中止();
}
否则{
而(!feof(fd)和&!feror(fd)){
fscanf(fd,“%s\t%d\n”,tmp->data.name,&tmp->data.id);
tmp->next=(node)malloc(sizeof(struct_node));
断言(tmp->next);
tmp=tmp->next;
l->size++;
if(tmp==NULL){
printf(“尝试分配内存时出错\n”);
中止();
}
}
}
tmp->next=NULL;
fclose(fd);
}
intmain(intargc,char*argv[]){
list l=list_create();
如果(argc!=2){
printf(“输入错误\n”);
}
载荷(argv[1],l);
\*一些代码*\
而(!list_empty(l)){
list_freenode(list_deletefirst(l));
}
免费(l);
返回0;
我希望能够成功加载文件,能够编辑其组件并保存它们。您的分段错误可能是因为您的函数
list\u create()
分配了一个数据结构,但您的函数load()
在文件中填充与行一样多的数据结构,从而在未分配的空间中写入数据。使用调试符号构建程序,并在调试器下运行。它会相当快地告诉您最直接的问题在哪里。除非您是list\u create
自动挂起足够多的节点,以包含整个文件e(这会很奇怪)根本问题是你从来没有为你的scanf分配要填充的节点。现在,关于调试器…请提供一个正确的(可读的)代码缩进。如果能看到list\u create
的代码,那就太好了。当我们没有所有东西时,很难说什么是错的。还有一些其他提示:不要在typedefs
后面隐藏指针。在你的情况下,你不必使用malloc
(或calloc
)要在堆上分配struct\u列表
。C
(和C++
)不是java
,您不必在堆上分配所有struct
(或类
)。另一方面,所有struct\u节点
必须(因为您不知道需要多少)分配malloc(或类似问题)。通常是在用数据填写列表时完成的(在你阅读的情况下),如果你已经问了相同的问题,那么可能会有重复的问题。