Text File

hello guys there are so many guides how to work with text files but lets say this is our text file named "Numbers.txt"

the file looks like this :

-----------------------
4545
455
22
178
878
788
99
2212458
------------------------

there are random numbers as you can see

how can i get line by line using for loop ? i have seen many guides like while(getline(myfile,std::string z)); etc but how can i get smth like

for (int i = 1; i<=10;i++){
cout<<LINE[i];//so every loop will output line number i so 1 2 3 4 5 to 10
}
Last edited on
Well, getline is normally the way to go if you want to grab a full line of text. Using the >> extraction operator can grab value by value, but keep in mind that the newline remains in the buffer, and doesn't get thrown out - so the next >> operation would get that instead of the next number.

i.e.

inputStream >> x; //Gets 4545
inputStream >>another; //Gets \n instead of a number.

Let me know if you need more help:
sparkprogrammer@gmail.com
1
2
3
4
5
6
	ifstream in("in.txt");
	int i;
	while(!in.eof()){
		in >> i;
		cout << i << endl;
	}
Topic archived. No new replies allowed.