问题
#include<stdio.h>
struct Node{
char *name;
int time;
int sec;
int x;
int y;
struct Node *next;
};
int main(){
FILE *file;
int index;
struct Node *data=malloc(sizeof(struct Node));
struct Node *tmp=data,*tmp2=data;
int enter;
file=fopen("data.txt","r");
if(file==NULL){
printf("File was not opened!\n");
return 0;
}
while(!feof(file)){
tmp=malloc(sizeof(struct Node));
fscanf(file,"%d",&index);
fscanf(file,"%d",&tmp->time);
fscanf(file,"%d",&tmp->sec);
fscanf(file,"%d",&tmp->x);
fscanf(file,"%d",&tmp->y);
fscanf(file,"%s",tmp->name);
fscanf(file,"%'\0",&enter);
tmp->next=NULL;
tmp=tmp->next;
}
fclose(file);
while(tmp2 != NULL){
printf("file:%d\t%d\t%d\t%d\t%s\n",tmp2->timestamp,tmp2->sec,tmp2->pointx,tmp2->pointy,tmp2->name);
tmp2=tmp2->next;
}
return 0;
}
Need some help with reading data from a file and write them to the linked list and after that print the linked list to the secreen.But it stops working immedately after I start the program.In the file data is like that:
- 1 28000 41 29 50 bbb
- 2 29000 91 19 60 ccc
- 3 30000 23 77 92 ddd
- 4 30000 37 62 65 eee
- 5 31000 14 45 48 fff
(there are tabs between them)
I read many questions but their answers didn't help me.I think I'm missing a point somewhere but I couldn't see the problem.Is it about reading a data directly to the linked list or something else? Thanks for help. //I'm looking at my code againt thanks for help**(edited)
回答1:
The name
field is a pointer, but you never allocate it. So
fscanf(file,"%s",tmp->name);
is undefined behavior. You really should be very scared. Take a few hours to read Lattner's blog: what every C programmer should know about undefined behavior.
Perhaps you might use getline(3) if on a POSIX system to read a line, or else use fgets(3).
BTW, when I compile your code with all warnings & debug info (with gcc -Wall -g
if using GCC) I'm getting many errors and warnings. You should improve your code till you get no warnings. Then you should use debugger (gdb
) to find many other bugs. You'll be able the run your buggy program step by step and query some variables. You should draw on a chalkboard (or on a paper) what you believe is the model and shape of the memory (including heap) of the process running your program. You'll find several bugs. Read more about C dynamic memory allocation and virtual address space.
Also, #include <stdlib.h>
is required when using malloc(3)
Please read the documentation of every function that you are using, notably for fscanf.
Notice that you forgot to handle failure of malloc
; and I also recommend using perror(3) in failure cases. So at least replace tmp=malloc(sizeof(struct Node));
with
tmp = malloc(sizeof(struct Node));
if (!tmp) { perror("malloc Node"); exit(EXIT_FAILURE); };
PS. I hope you don't expect us to do your homework. Your program is extremely buggy, but you'll learn a lot by finding by yourself these bugs. Asking someone else is counterproductive.
来源:https://stackoverflow.com/questions/40449544/reading-a-data-from-a-file-and-writing-it-to-a-linked-list