#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)