Skip to content

Instantly share code, notes, and snippets.

@gchudnov
Created November 6, 2014 19:36
Show Gist options
  • Save gchudnov/3ab5c398079202754306 to your computer and use it in GitHub Desktop.
Save gchudnov/3ab5c398079202754306 to your computer and use it in GitHub Desktop.
Read a text file
#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