代碼:
/* * main.cpp * * Created on: 2014年6月17日 * Author: Spike */ /*eclipse cdt, gcc 4.8.1*/ #include <iostream> #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); } int main (void) { std::cout << get_file_contents("跳河的兔子.txt") << std::endl; return 0; }