Reading line by line

I know how to read a .txt file line by line using ifstream, but I need a specific action this time. Let's this be my input file:

1 2
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5


First, I need to read the first line and store the values. For example, let's say I have 2 int variables(a and b) and I will store 1 at a and 2 at b. Storing is easy but I couldn't figure out how can I read a line in this way. Because, if I store the whole line, I don't think I will be able divide 1 and 2 after that.

I want to store the remaining lines as matrix, but I think it can be done with the same way I should use at first line. Can you guys help me with this?
Last edited on
you can split it, but that is annoying. just like on the console, whitespace breaks it up..

thefile>> a;
thefile>>b;
for(... all the rows r++)
for(... all the cols c++)
thefile>> m[r][c];

you may or may not want to error check your file as you read to ensure the file had data for all the cells. for now get it working with a good file, you can work on handling broken files after if you want.
In an outer loop read each line as a std::string. Using std::getline.

http://www.cplusplus.com/reference/string/string/getline/

Convert each read string into a std::stringstream.

http://www.cplusplus.com/reference/sstream/stringstream/

In another, inner loop extract each number in the string into an int. When all the numbers in the string are read exit the inner loop and continue looping the outer loop until all the file lines have been read and processed.
How you read it depends on the format of the input file. For example, if you knew exactly how many items to read then you could read them directly with a stream extractor and there would be no need to go through the conversions implied by using getline.

So what ARE the first two items in your input file? If the rest of the file represents a matrix then I would expect them to be, say, the number of rows and columns. However, that plainly isn't the case in your example.

So, what exactly is the format of your input file?
So what ARE the first two items in your input file? If the rest of the file represents a matrix then I would expect them to be, say, the number of rows and columns.

You can think that first one is row number, and second one is column number. It is little longer, but it just does another operations at the same matrix.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
using namespace std;

int main()
{
   int rows, cols;

// ifstream in( "data.txt" );
   stringstream in( "3  4                      \n"       // simulated file for demo
                    "1.0    2.0    3.0    4.0  \n"
                    "11.1   22.2   33.3   44.0 \n"
                    "2.0    4.0    6.0    8.0  \n" );

   // Read from input stream
   in >> rows >> cols;
   vector< vector<double> > M( rows, vector<double>( cols ) );
   for ( int i = 0; i < rows; i++ )
   {
      for ( int j = 0; j < cols; j++ ) in >> M[i][j];
   }

   // Write out as a check
   for ( auto &R : M )
   {
      for ( auto e : R ) cout << e << '\t';
      cout << '\n';
   }
}


1	2	3	4	
11.1	22.2	33.3	44	
2	4	6	8
Topic archived. No new replies allowed.