Loading text from file into struct

This is a discussion on Loading text from file into struct within the C++ Programming forums, part of the General Programming Boards category; I was hoping someone could give me a hand or point me in the right direction I have a text ...

  1. #1
    Unregistered
    Guest

    Loading text from file into struct

    I was hoping someone could give me a hand or point me in the right direction

    I have a text file which consists of records (comma delimited)
    1)First Name
    2)Last Name
    3)Age
    4)Children's name (if any)

    example text file:

    Joe, White, 32, Karen, Bill
    John, Doe, 45
    Bill, Jones, 29, Steve

    I have no problem opening the text file and reading the data into a vector of strings.

    But now I want to create a struct called person

    struct person
    {
    string first;
    string last;
    int age;
    vector <string> children;
    };

    and create a vector of structs which will be filled with the information from the file.

    I was trying to use find and substring functions but with no luck. This is my first programming experience and really only have a grasp of the fundamentals (haven't implemented classes yet)

    Any help would be greatly appreciated.

  2. #2
    Registered User xds4lx's Avatar
    Join Date
    Nov 2001
    Posts
    630
    Ok your on the right track using find and substr methods of the string class. I would say that you should do something kinda like this :
    Code:
    vector<string> file;
    ....
    ....
    person a_person;
    vector<person> 
    
    while(int i < file.size())
    {    int iCommaPos1 = file[i].find(","); 
         // or alternitively you could manually search by using a loop 
         a_person.first = file[i].substr(0,iCommaPos);
         
         int iCommaPos2 = file[i].find(",",iCommaPos1+1); // tells it to
         // searching from right after the last comma
         a_person.last = file[i].substr(iCommaPos1+1,(iCommaPos2-iCommaPos1)); // this is because the length of this substr is the 
                  // difference between the two positions
    
        int iCommaPos3 = file[i].find(",",iCommaPos2+1);
        a_person.age = file[i].substr(iCommaPos2+1,(iCommaPos3-iCommaPos2));
    
        if(iCommaPos3 < file[i].length())
        {  int iCommaPosC = file[i].find(",",iCommaPos3+1);
           if(iCommaPosC > file[i].length() || iCommaPosC < 0)
               children.push_back(file[i].substr(iCommaPos3+1,(file[i].length() - iCommaPos3)));
           else
           {  chilldren.push_back(file[i].substr(iCommaPos3+1,(iCommaPosC - iCommaPos3)));
              ........
              // repeat the test again to see if this is the last comma
              // if searching for a nother fails, if so then your finished
              // if not then you do another push_back like the one i used
              // in the else, this code might not be perfect you might
              // have to adjust the substr's a little its been a while 
              // since ive done this
    "only two things are infinite, the universe and human stupidity, and im not sure about the former." - albert einstein

  3. #3
    Unregistered
    Guest
    Thank you, your reply was more than I hoped for.

    I have a lot to think about.

    Appreciate it!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting from C to C++
    By Taka in forum C++ Programming
    Replies: 5
    Last Post: 04-08-2009, 02:16 AM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 04:02 AM
  3. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  4. Replies: 10
    Last Post: 05-18-2006, 11:23 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM

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