Last active
December 28, 2023 22:08
-
-
Save stevedoyle/1319089 to your computer and use it in GitHub Desktop.
Reading a text file in C++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
what if I use
////
while (!myfile.eof())
////
then how can I read