Using sscanf to read in the file, Help please!

This is a discussion on Using sscanf to read in the file, Help please! within the C Programming forums, part of the General Programming Boards category; Hello all, I have something like this: typedef void COMMAND (void); typedef struct { int ID; int copies; int period; ...

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    54

    Using sscanf to read in the file, Help please!

    Hello all,

    I have something like this:

    typedef void COMMAND (void);

    typedef struct
    {
    int ID;
    int copies;
    int period;
    int InUse;
    COMMAND *function;
    } resource;


    resource resourceTable[6] = {
    {A, 5, 6, 0, ProcessA},
    {B, 2, 10, 0, ProcessB},
    {C, 1, 0, 3, NULL},
    {D, 1, 2, 0, NULL},
    {E, 1, 2, 0, NULL},
    {F, 3, 4, 0, NULL},
    };

    enum
    {
    A = 0,
    B,
    C,
    D,
    E,
    F
    }sources

    Now I have a different definition file called definition.h:

    A 5 6
    B 2 10
    C 1 0
    D 1 2
    E 1 2
    F 3 4


    Now in Main(), I want to read in the resourceTable from definition.h and Use sscanf function. Would anyone please help me to do it? Thank you very much!

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    1) sscanf is for scanning other strings, not files. Use fscanf.

    2) Don't call your files that contain data ".h" files. ".h" files are header files that are #include-ed. Call it something else.

    3) Learn to use fscanf from the link provided, then if you get stuck, post your attempt and we'll help you out.

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

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    54
    o.k. I will

    #include<stdio.h>
    #include "definition.h"

    int main(void)
    {
    FILE *fp;
    fscanf(fp, "%s %d %d",resourceTable.ID, copies, period);

    ///some code
    return 0;
    }

    is that right?
    I don't know how to pass the argument in the () of fscanf. Please help!
    Thanks!
    Last edited by vnrabbit; 09-04-2002 at 03:28 PM.

  4. #4
    Registered User
    Join Date
    Sep 2001
    Location
    Australia
    Posts
    212
    You need to open the file first,
    typedef void COMMAND (void);
    what the hell is that, typedef is used to give a data type an alias name eg typedef int bool. This is valid in C because C doesn't have a builtin data type of bool.

  5. #5
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Originally posted by kwigibo
    You need to open the file first,


    what the hell is that, typedef is used to give a data type an alias name eg typedef int bool. This is valid in C because C doesn't have a builtin data type of bool.
    He's defining a function pointer and putting it as a member of his struct.......

    But if he's going down that road, he's better of using C++

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File Writing Problem
    By polskash in forum C Programming
    Replies: 3
    Last Post: 02-13-2009, 10:47 AM
  2. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 05:14 AM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. c script help to read a file & update
    By indy in forum C Programming
    Replies: 8
    Last Post: 12-01-2003, 11:32 AM
  5. Hmm....help me take a look at this: File Encryptor
    By heljy in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 10:57 AM

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