Read text file into char Array. C++ ifstream
std::ifstream infile;
infile.open("Textfile.txt", std::ios::binary);
infile.seekg(0, std::ios::end);
size_t file_size_in_byte = infile.tellg();
std::vector<char> data; // used to store text data
data.resize(file_size_in_byte);
infile.seekg(0, std::ios::beg);
infile.read(&data[0], file_size_in_byte);
Use std::string
:
std::string contents;
contents.assign(std::istreambuf_iterator<char>(infile),
std::istreambuf_iterator<char>());
You don't need to read line by line if you're planning to suck the entire file into a buffer.
char getdata[10000];
infile.read(getdata, sizeof getdata);
if (infile.eof())
{
// got the whole file...
size_t bytes_really_read = infile.gcount();
}
else if (infile.fail())
{
// some other error...
}
else
{
// getdata must be full, but the file is larger...
}