Thread: Read colums from a file to inser them in structs

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    1

    Wink Read colums from a file to inser them in structs

    Hello

    I must read information form a text file and i am currently using the function strtok to separete the information that is in colums to insert it into a structure in the program.
    Anyway, i think that's not the best and simple way to do it, so i wonder how it could be done.

    I have something like this: (colums separate by \n and lines by \n)
    field1\field2\field3\field4\n
    field1\field2\field3\field4\n
    field1\field2\field3\field4\n
    field1\field2\field3\field4\n

    Each line would be in a structure and each field in an element of each structure.

    I think there must be a better way to trade with this problem as this text organization is very used in logs or small things that doesnt requiere from any BD.

    Thank you

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    I don't see any problem using strtok. Alternatively if you know for certain that there are only 4 fields on each line you could read everything in one string and parse it yourself. However, that is basically the same thing as strtok-ing it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File read in problem
    By DimensionX in forum C Programming
    Replies: 5
    Last Post: 12-02-2009, 12:37 PM
  2. read a file of commands and execute them
    By surlyTomato in forum C Programming
    Replies: 5
    Last Post: 08-20-2009, 11:32 AM
  3. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  4. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM