Thread: Reading a text file into multiple nodes

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    3

    Unhappy Reading a text file into multiple nodes

    Hi there,

    I'm undergoing my first year of study in C programming. I was wondering if anybody could point me in the right direction for some homework I have.

    I've been given a text file, which contains data that needs to be sorted into nodes and then arranged into a linked list.

    So, say the data is in this format:

    age: 32
    day: Thursday
    country: Australia
    about: My favourite food is spaghetti
    and I really like cats

    Love Anna

    age: 67
    day: Monday
    country: New Zealand

    .. and so on.

    At the moment, entire text file is opened and read as a string.
    I need to read '32' and assign it to node->age, and so on for all the other values. I've written the structure for creating nodes etc. What I'm completely stumped on is how I tell the program what text to store where. In other words, how do I tell it to store '32', 'Thursday' and 'Australia'
    Do I have to traverse trough the txt file until the word 'age:' is found? And if so, how do I then tell the program when to finish reading the text and move on to the next set of data for the next node?

    Thanks in advance

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    while not done
        read age
        read country
        read about
        ...figure out how you've decided an entry ends
    
        put all that in a new node
        stick that node on a list
    There are plenty of posts here about linked lists if you aren't sure how to make one. Try searching.

    Read a line, see if it starts with "age:" or "day:" or "country:" ... if it doesn't, and it isn't blank, then stick it in your 'other' field, or whatever it's going to be called.


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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Advice reading lines from a text file.
    By Fujitaka in forum C Programming
    Replies: 2
    Last Post: 08-11-2009, 09:43 PM
  2. Reading issue, mixed text file
    By hawaiian robots in forum C Programming
    Replies: 7
    Last Post: 05-22-2009, 04:38 AM
  3. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Reading a text file
    By AmazingRando in forum C Programming
    Replies: 2
    Last Post: 09-08-2003, 10:38 AM