18

I have a text file with on every line one or more integers, seperated by a space. How can I in an elegant way read this with C++? If I would not care about the lines I could use cin >>, but it matters on which line integers are.

Example input:

1213 153 15 155
84 866 89 48
12
12 12 58
12
jogojapan
  • 68,383
  • 11
  • 101
  • 131
Peter Smit
  • 27,696
  • 33
  • 111
  • 170
  • Are you saying you need to read one line at a time? That you need to know where the line breaks are, not just the sequence of numbers? –  Feb 18 '10 at 08:18
  • @Steve314 Indeed, I already realized that I forgot to mention that – Peter Smit Feb 18 '10 at 08:21

4 Answers4

32

It depends on whether you want to do it in a line by line basis or as a full set. For the whole file into a vector of integers:

int main() {
   std::vector<int> v( std::istream_iterator<int>(std::cin), 
                       std::istream_iterator<int>() );
}

If you want to deal in a line per line basis:

int main()
{
   std::string line;
   std::vector< std::vector<int> > all_integers;
   while ( getline( std::cin, line ) ) {
      std::istringstream is( line );
      all_integers.push_back( 
            std::vector<int>( std::istream_iterator<int>(is),
                              std::istream_iterator<int>() ) );
   }
}
David Rodríguez - dribeas
  • 204,818
  • 23
  • 294
  • 489
  • The second part is *exactly* what I needed! Thanks for everyone who answered! – Peter Smit Feb 18 '10 at 08:28
  • I needed to put istream_iterator(is) inside '(' ')' to avoid ambiguous syntax ( apparently it can be confused with a function too, clang warned me ). – Kknd Aug 21 '14 at 13:33
  • @David Rodríguez - dribeas, thanks. I don't understand the syntax of one line in the second part. //std::vector( std::istream_iterator(is),std::istream_iterator() ), it seems you declare and initialize a new vector in the same time here, could you explain a little more about it? especially on"std::istream_iterator() " ? Thanks! – ulyssis2 Oct 15 '15 at 22:23
  • @ulyssis2: The syntax `T()` inside an expression creates a temporary of type `T` that is *value-initialized*, the similar syntax `T(args)` creates a temporary of type `T` initialized with `args`. This is kind of basic syntax. There is a list of recommended books for C++ in S.O. do look at them. – David Rodríguez - dribeas Oct 16 '15 at 08:36
7

You could do smtng like this(I used cin, but you can use any other file stream):

string line;
while( getline( cin, line ) )
{
 istringstream iss( line );
 int number;
 while( iss >> number )
  do_smtng_with_number();
}

Or:

int number;
while( cin >> number )
{
 do_smtng_with_number();
}
synepis
  • 1,292
  • 16
  • 28
5

What result do you want? If you want all the integers in a single vector, you could do something like:

std::ifstream input("input.txt");

std::vector<int> data(std::istream_iterator<int>(input),
                      std::istream_iterator<int>());

That discards the line-structure though -- you end up with the data all together. One easy way to maintain the original line structure is to read a line with getline, initialize a stringstream with that string, then put the values from that stringstream into a vector (and push that onto the back of a vector of vectors of int).

std::vector<std::vector<int> > data;
std::vector<int> temp;

std::string t;
while (std::getline(input, t)) {
    std::istringstream in(t);
    std::copy(std::istream_iterator<int>(in), 
              std::istream_iterator<int>(), 
              std::back_inserter(temp);
    data.push_back(temp);
}
Alrekr
  • 300
  • 4
  • 14
Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
0

Here you go :

void readFromFile(string filename)
{
    string line;
    ifstream myfile(filename);
    if (myfile.is_open())
    {
         while ( getline(myfile,line) )
    {
        cout << line << '\n';
    }
        myfile.close();
  }
}

int main(int argc, char* argv)
{
    readFromFile("Input.txt");
    getchar();
    return 0;
}
Desai
  • 275
  • 3
  • 7