Thread: Load Strings into Vectors From a File

  1. #1
    Registered User
    Join Date
    Apr 2014
    Posts
    2

    Load Strings into Vectors From a File

    Okay, so I'm working on a project, and I'm trying to fill in various vectors from a given input file. The input file looks like:
    <catalog>
    <book id ="bk101">
    <author>O'Brien, Tim</author> ....etc
    My load vectors function looks like this: void load_vectors(vector<string>&id, vector<string>& author...etc)

    I can't assume a limit on the number of books etc listed in this catalog, so I'm using the eof() function. However, I don't know how to write the loop to gather the correct strings and place them in the vectors.
    Code:
    while(in.eof())
    { string text;
    int index, index2;
    
    getline(in, text);
    int index = text.find("<author>");
    int index2 = text.find("</author>");
    a = index.lenght();
    author.pushback[i] = text.substr(index + a, index2);
    }
    Thank you for any feedback. Example codes would be extremely helpful.

  2. #2
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    Quote Originally Posted by Ralek View Post
    Code:
    while(in.eof())
    { string text;
    int index, index2;
    
    getline(in, text);
    int index = text.find("<author>");
    int index2 = text.find("</author>");
    a = index.lenght();
    author.pushback[i] = text.substr(index + a, index2);
    }
    *) You declare index and index2 twice.
    *) Suppose to be "length", not "lenght".
    *) "index" is an integer. Why are you calling length on an integer?
    *) Suppose to be push_back(), not pushback[i]. What does "i" even mean?
    *) Using "getline" for this won't work very well if the <author/> tags aren't on the same line.
    *) substr()'s second argument is length, not an offset.
    *) What the hell are you doing?

  3. #3
    Registered User
    Join Date
    Apr 2014
    Posts
    2
    Sorry, lol. Everything you said is actually fixed in my code, but I typed this out here just for the concept. I didnt actually mean for that to be my code. I'm just confused on where to put a loop.

  4. #4
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    Quote Originally Posted by Ralek View Post
    Sorry, lol. Everything you said is actually fixed in my code, but I typed this out here just for the concept. I didnt actually mean for that to be my code. I'm just confused on where to put a loop.
    Ah, kk. Post your (non-)working code! Someone/I will be able to tell you why it's not working, and where you should go from there.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to load/run file
    By Inkinsarto in forum C++ Programming
    Replies: 5
    Last Post: 10-01-2013, 11:13 AM
  2. Some questions with strings, vectors, references...
    By samus250 in forum C++ Programming
    Replies: 11
    Last Post: 07-10-2008, 02:31 PM
  3. load matrix into a vector of vectors
    By acosgaya in forum C++ Programming
    Replies: 1
    Last Post: 10-21-2006, 03:06 PM
  4. Vectors and strings
    By Macplus in forum C++ Programming
    Replies: 2
    Last Post: 12-12-2002, 09:29 PM
  5. how to load file in C#
    By SuperNewbie in forum C# Programming
    Replies: 1
    Last Post: 07-04-2002, 04:41 AM