Using fscanf with a structure

This is a discussion on Using fscanf with a structure within the C Programming forums, part of the General Programming Boards category; I don't really use fscanf, so I need some help here. I need to read in data from file like ...

  1. #1
    Registered User daluu's Avatar
    Join Date
    Dec 2002
    Posts
    42

    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.

  2. #2
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Beware of naming instances of structs that similarly to the type name. Especially when you end up declaring pointers of type PCB, when PCB is an instance.

  3. #3
    Registered User daluu's Avatar
    Join Date
    Dec 2002
    Posts
    42
    ??? so you mean I should use "pcb" for typecasting or rename PCB to remove possible confusions?

  4. #4
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Both

    edit: I just saw the "typedef" before your structure definition - and it's been too long since I used typedef for me to know what I'm talking about here, so just disregard what I just said, unless someone who uses C more confirms it.
    Last edited by sean; 10-10-2004 at 06:52 PM.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by daluu
    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.
    No. You don't dereference pointers to scan into them for fscanf. You give the address-of the variable you want to scan into. You do know how to scan into an int, right?
    Code:
    fscanf( foo, "%d", &myint );
    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    > while(!feof(dfile)){
    FAQ
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User daluu's Avatar
    Join Date
    Dec 2002
    Posts
    42
    quzah, I know that but I'm not good at passing addresses of members within a structure. If pid were by itself I would use &pid. But how do I do it within the context of plist->pid?

    I guess I can resort to using a temp variable for fscanf and then pass that to the structure.

    salem, I'm not sure what your comment means.

  8. #8
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    consult the faq as to why using feof () for loop control is bad.
    hello, internet!

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    You can lead a horse to water, but you can't make him think
    http://faq.cprogramming.com/cgi-bin/...&id=1043284351
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  10. #10
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by daluu
    If pid were by itself I would use &pid. But how do I do it within the context of plist->pid?
    &plist->pid
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I had thought with the illustration of using scanf, that you'd be able to put two and two together. Apparently not.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fscanf in different functions for the same file
    By bchan90 in forum C Programming
    Replies: 5
    Last Post: 12-03-2008, 08:31 PM
  2. Problem referencing structure elements by pointer
    By trillianjedi in forum C Programming
    Replies: 19
    Last Post: 06-13-2008, 05:46 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 09:52 AM
  5. C structure within structure problem, need help
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 11-30-2001, 04:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21