5,321
edits
Line 63: | Line 63: | ||
} | } | ||
return 0; | return 0; | ||
} | |||
</syntaxhighlight> | |||
====Reading a whole file==== | |||
[https://insanecoding.blogspot.com/2011/11/how-to-read-in-file-in-c.html Reference and comparison of different methods] | |||
<syntaxhighlight lang="C++"> | |||
#include <fstream> | |||
#include <string> | |||
#include <cerrno> | |||
std::string get_file_contents(const char *filename) | |||
{ | |||
std::ifstream in(filename, std::ios::in | std::ios::binary); | |||
if (in) | |||
{ | |||
std::string contents; | |||
in.seekg(0, std::ios::end); | |||
contents.resize(in.tellg()); | |||
in.seekg(0, std::ios::beg); | |||
in.read(&contents[0], contents.size()); | |||
in.close(); | |||
return(contents); | |||
} | |||
throw(errno); | |||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> |