Created
November 6, 2014 19:36
-
-
Save gchudnov/3ab5c398079202754306 to your computer and use it in GitHub Desktop.
Read a text file
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
#include <iostream> | |
#include <string> | |
#include <fstream> | |
int main() { | |
std::string filepath = "data.txt"; | |
std::ifstream ifs(filepath.c_str(), std::ios_base::in | std::ios_base::binary); | |
if (ifs) { | |
ifs.seekg(0, std::ios_base::end); | |
size_t length = static_cast<size_t>(ifs.tellg()); | |
ifs.seekg(0, std::ios_base::beg); | |
std::string text; | |
text.resize(length); | |
ifs.read(&text[0], length); | |
std::cout << text << std::endl; | |
ifs.close(); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment