Thread: To Parse a file and save the information in a linklist.

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    4

    To Parse a file and save the information in a linklist.

    I wish to write a code which parses a file which contains the following info (say)
    (some electrical engineering problem)

    R1 1 2 20
    V1 1 2 40
    R2 4 5 35
    V2 4 5 60
    ..
    .
    .
    (n such lines)

    i have to incorporate doubly linked link lists
    where the delimiters being the whitespaces,
    i have to construct n such nodes with the corresponding elements going as the data variables in the linked-list stucture which i would define earlier.
    i can parse the file.. but am not able to correctly use the strtok() function to separate the elements and inserting them into the proper structure data variables




    How to go about it? some codes would be highly appreciated

  2. #2
    Registered User
    Join Date
    Sep 2012
    Posts
    4
    The stucture that i am defining is
    Code:
    typedef struct netlist
      {
       char *name, *value;
       char *n1,n2;
       struct *next, *prev;
      }nlist;

  3. #3
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Why do you want to parse the numbers as strings ?
    Kurt

  4. #4
    Registered User
    Join Date
    Sep 2012
    Posts
    4
    n1 and n2 are not any values as such
    They are just for marking some locations in an electric circuit
    Code:
    typedef struct netlist
      {
       char *name;
       char *n1,*n2;
       float value;
       struct *next, *prev;
      }nlist;
    thing is some 'n1 and n2 's' would also have names like a1, a2 later on instead of the 1 2 i have given in the first post

  5. #5
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    To simplify things I would suggest to use char arrays instead of char pointers.
    e.g.
    Code:
    typedef struct netlist   {
       char name[30];
       char n1[10], n2[10];
       float value;
       struct netlist *next, *prev;
    } nlist;
    This way allocation and deallocation of your list nodes is much simpler.

    Also I wouldn't use strtok() to parse the input.
    Using fgets() to read a line of input and parsing the string with sscanf() seams easier.for me.

    Kurt

  6. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Do you know how to parse a line like that from stdin, and how to build a link list? You can roughly break it down in those two components imo. Create a function that takes an unparsed line then parse it and return a new node with all fields filled in, or NULL if you where not able to parse it.

    Then create a linked list.

    You can then use these components together in main:

    • read a line
    • parse it to new node
    • insert in list

  7. #7
    Registered User
    Join Date
    Sep 2012
    Posts
    4
    Thanks guys got it.. finished with the coding.!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How do I save my parse responses to a text file?
    By Karyumi in forum C Programming
    Replies: 1
    Last Post: 07-19-2012, 09:08 PM
  2. How do I parse an XML file?
    By AlecTaylor in forum C++ Programming
    Replies: 7
    Last Post: 11-09-2011, 03:19 AM
  3. Replies: 12
    Last Post: 04-01-2008, 08:58 AM
  4. Struct/parse returning wrong information
    By dontknowc in forum C Programming
    Replies: 5
    Last Post: 12-04-2007, 01:39 PM
  5. Replies: 1
    Last Post: 12-26-2001, 03:00 AM