Thread: How do i change this code to take elements from file and save in an integer vector?

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    33

    How do i change this code to take elements from file and save in an integer vector?

    How can i write code to take integers from a text file and place them into a vector that holds integers?

    Code:
    std::vector<std::string>  elementVector;
        std::ifstream in("filename.dat");
        std::string line;
        while( std::getline(in, line) )
        {
            if( line == "<et>" )
            {
                std::getline(in,line);
                stringstream st;
                st << line;
                std::string word;
                while( std::getline(st,word,'\'' ))
                {
                    size_t pos = word.find('\'');
                    if( pos != string::npos )
                        word.erase(pos,1);
                    elementVector.push_back(word);
                }
     
            }
        }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Work on converting the contents of word to an integer such that you can change the push_back to append an integer to a vector of integers.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    33
    I was trying to do all that stuff, and i did not find append integer......

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Use another stringstream, initialised with your word, then do

    st >> myInt;
    myIntVector.push_back(myInt);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting string vector to integer vector
    By CPlus in forum C++ Programming
    Replies: 4
    Last Post: 05-08-2010, 05:43 AM
  2. Save vector to file...
    By IndioDoido in forum C++ Programming
    Replies: 1
    Last Post: 10-18-2007, 04:09 PM
  3. how to add save file func into this code!!
    By sam3291 in forum C++ Programming
    Replies: 5
    Last Post: 04-03-2002, 11:19 PM
  4. How to save a source code file in DOS.
    By csick in forum C Programming
    Replies: 5
    Last Post: 01-24-2002, 10:10 AM
  5. Joining array elements and save in variable.
    By Nutshell in forum C Programming
    Replies: 12
    Last Post: 01-12-2002, 01:06 AM