Thread: fscanf parameter help

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    5

    fscanf parameter help

    Hi! Been reading this board for awhile, and cannot figure something out.

    I'm using fscanf to read multiple lines from a text file... however some lines have less information then the others. I.E:

    A 344532111 S SPY 300 117.880000
    R 344532111 SPY 300 117.840000
    T 344532111 SPY 100

    I need to store in a struct, so I can create a linked list out of it, but I want to set certain variables in the struct to null if it's not in the line. Any efficient way of doing this? Right now I'm only able to correctly store the first line, since it has all the properties I'm using the fscanf for. fscanf(fp,"%s %d %s %s %d %lf",.....);
    Thanks for any help, let me know if you need me to post any further code.

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    C isn't really all that great for this.

    You can read an entire line with fgets() and then use sscanf() to pull it apart. Check the return value of sscanf() to see how many values were converted.

    Alternatively, you could read an entire line with fgets() and break it apart with strtok(), which can allow for better control over the conversion/checking of each record... but it's not really all that fun.

    In either case, it's up to you to manually set unused elements to an invalid value.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    5
    are there any examples of parsing lines with a different amount of words using fgets and sscanf/strtok?

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I assume there is some significance as to what the first letter of the line means. If so, that should make it easy. Call a different *scanf based on the first letter of each line.


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

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    5
    oh you're right! I just realized that. how can you get the first char of the line?

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I third the suggestion to use
    fgets(yourCharBuffer, sizeof(yourCharBuffer, yourFilePointer);

    then
    yourCharBuffer[0] is the first char in the line. (so is just yourCharBuffer, but we won't go into that).

    It appears the spaces separate the fields in this record, so counting the spaces in your buffer would be another way to do this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with fscanf
    By dford425 in forum C Programming
    Replies: 8
    Last Post: 01-16-2011, 06:44 PM
  2. fscanf help
    By snielsen in forum C Programming
    Replies: 16
    Last Post: 09-03-2009, 12:41 PM
  3. Replies: 6
    Last Post: 01-08-2008, 10:25 AM
  4. fscanf and EOF
    By .ZG. in forum C Programming
    Replies: 3
    Last Post: 05-30-2004, 07:49 AM
  5. fscanf on sun's
    By brif in forum C Programming
    Replies: 2
    Last Post: 04-14-2002, 01:22 PM