Skip to content

Instantly share code, notes, and snippets.

@stevedoyle
Last active December 28, 2023 22:08
Show Gist options
  • Save stevedoyle/1319089 to your computer and use it in GitHub Desktop.
Save stevedoyle/1319089 to your computer and use it in GitHub Desktop.
Reading a text file in C++
// Taken from http://www.cplusplus.com/doc/tutorial/files/
// reading a text file
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main () {
string line;
ifstream myfile ("example.txt");
if (myfile.is_open())
{
while ( myfile.good() )
{
getline (myfile,line);
cout << line << endl;
}
myfile.close();
}
else cout << "Unable to open file";
return 0;
}
@SteinStyler
Copy link

Thanks for uploading the code. It helped me very much to reach my target. Thanks!

@Jezzzae
Copy link

Jezzzae commented Apr 4, 2020

what if I use
////
while (!myfile.eof())
////
then how can I read

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment