Monday, 15 June 2015

c++ - easiest way to read a file line by line? -



c++ - easiest way to read a file line by line? -

this question has reply here:

c++: reading file line line 3 answers

i have file this

joshua 21 234 5 40.9 jones 14 403 4 39.2

now want know easiest way tell compiler read each line , print on screen? please seek reply using standard c++ libraries , not creating custom function. please no vectors. far have tried this

ifstream myfile; string line; { cout << getline(myfile,line); }while(!myfile.eof()); break;

it simple. utilize fstream open file , read line line.

here example.

#include <iostream> #include <fstream> int main() { std::ifstream file_in; file_in.open("test.txt", std::ios::in); if(file_in.fail()) { std::cout << "error: unable open test.txt.\n"; return(1); } char c; while(!file_in.fail() && !file_in.eof()) { file_in.get(c); std::cout << c; } file_in.close(); return(0); }

this uses std , allow read line line.

c++

No comments:

Post a Comment