file handling in C++


I've a text file of multiple lines like this :

12 12 236 124
13 547 25 36
14 15 12 36 36 347
etc

How do i use file handling to "know" when the enter has been pressed, that is I've arrived at the next line.
I tried something like
1
2
3
4
5
6
7
8
9
10
11
12
13
14
char c = ' ';
while( t--)
    {
        count = 0;
        while( c != '\n' )
        {
           
            infile >> a[i++];
             c = infile.get();       // get character from file
        }
       // i need to exit the inner loop after each line
       c = 'r';

    }

But this doesn't quite work. In the above code the inner loop ( which is supposed to break when i get to the next line) doesn't break till the end of the file. Can someone please help me and tell me fix this or some other way to do it>
Thank you very much.
closed account (Dy7SLyTq)
1
2
while(getline(infile, line))
	cout<< line << endl;


where line is std::string
I've to actually store the numbers in the first line in the first array, second line in the second array and so on.
How do i do that?
Use a stringstream to get the individual values on each line.
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
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>

using namespace std;

int main()
{
    ifstream infile("input.txt");
    string line;
    
    while (getline(infile, line))
    {
        istringstream ss(line);
        int num;
        while (ss >> num)
        {
            cout << num << " ";
        }
        cout << endl;
    }
    
    return 0;
}
Yes research sstream, you can read the line to a string, associate the string with a string stream and read/process them individually.
Topic archived. No new replies allowed.