5

What's the standard way of reading a "line of numbers" and store those numbers inside a vector.

file.in
12 
12 9 8 17 101 2 

Should I read the file line by line, split the line with multiple numbers, and store the tokens in the array ?

What should I use for that ?

1
  • If you can guarantee they will be int's, you can simply read them and store them into an integer array (or an STL data structure like vector would work) granted that you explain how to parse with whatever delimiters you are using. If the output could be something else, you will probably want to read things into a string and use stringstream to convert them to integers. Commented Feb 15, 2011 at 15:10

3 Answers 3

5
#include <vector>
#include <fstream>
#include <iterator>
#include <algorithm>

std::vector<int> data;
std::ifstream file("numbers.txt");
std::copy(std::istream_iterator<int>(file), std::istream_iterator<int>(), std::back_inserter(data));
Sign up to request clarification or add additional context in comments.

Comments

4

Here's one solution:

#include <fstream>
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <iterator>

int main()
{
    std::ifstream theStream("file.in"); 
    if( ! theStream )
          std::cerr << "file.in\n";
    while (true)
    {
        std::string line;
        std::getline(theStream, line);
        if (line.empty())
            break;
        std::istringstream myStream( line );
        std::istream_iterator<int> begin(myStream), eof;
        std::vector<int> numbers(begin, eof);
        // process line however you need
        std::copy(numbers.begin(), numbers.end(),
                  std::ostream_iterator<int>(std::cout, " "));
        std::cout << '\n';
    }
}

5 Comments

The codes are beautiful! I have tested it and it works, but I don't understand one line (I am new to C++), could you please explain what does this line do? std::istream_iterator<int> begin(myStream), eof; Are begin and eof are two iterators? Thanks!
@ulyssis2: Yes, begin is an istream_iterator that will read from myStream, and eof is an istream_iterator that represents the end of the file.
thank you very much for answering me, I have a followup question, as eof is just declared and not initialized, how can it denote the end of the file?
eof is default initialized. And the spec says that a default-initialized istream_iterator represents the end-of-stream iterator.
@HowardHinnant This answer really helped me in my project +1. Just wondering how to manipulate the numbers in detail. For instance, I'm not too sure how to access the 4th number on the second line ... or the nth number on the mth line of my file, from this solution. Regards
1

std::cin is the most standard way to do this. std::cin eliminates all whitespaces within each number so you do

while(cin << yourinput)yourvector.push_back(yourinput)

and they will automatically be inserted to a vector :)

EDIT:

if you want to read from file, you can convert your std::cin so it reads automatically from a file with:

freopen("file.in", "r", stdin)

4 Comments

are you sure that freopen plays nice with std::cin?
I am pretty sure since no error comes to me until now.. except that freopen throws exception with the non existence of the file
Since when goes freopen() throw exceptions!?
std::cin does not eliminate the witespace, the >> operator does that. The pseudocode is wrong (why not just post real code?). Also reopening the standard input is not a good way to read files. Just use a std::ifstream and forget about std::cin.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.