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

This is a discussion on How do i change this code to take elements from file and save in an integer vector? within the C++ Programming forums, part of the General Programming Boards category; How can i write code to take integers from a text file and place them into a vector that holds ...

  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
    21,448
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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, 10: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, 09:10 AM
  5. Joining array elements and save in variable.
    By Nutshell in forum C Programming
    Replies: 12
    Last Post: 01-12-2002, 12:06 AM

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