Using fscanf with a structure
I don't really use fscanf, so I need some help here.
I need to read in data from file like this:
A 0 7 B 24 67 C 67 6 D 82 9 E ...
letters should be char & #s be int, and I'm using this structure:
Code:
typedef struct pcb{
char pid;
int arrivetime;
int jobtime;
int timeleft;
int cputime;
int waitime;
struct pcb *next;
}PCB;
PCB *plist = NULL; //later points to structs in heap,etc.
so this is what I'd like to do:
Code:
while(!feof(dfile)){
fscanf(dfile,"%c",plist->pid);
fscanf(dfile,"%d",plist->arrivetime);
fscanf(dfile,"%d",*(plist->jobtime));
plist->next = new PCB; //make next PCB & update list
}
But that doesn't seem to work. I get runtime error with 1st fscanf line. the others don't seem to read the value in at all.
I've tried writing it this way but no good:
Code:
fscanf(dfile,"%c",((PCB *)plist)->pid);
fscanf(dfile,"%c",*(PCB *).(char *)plist.pid);
What's the right way to use fscanf in this scenario? Or should I use some other method? I definitely don't want to manually parse the data.