c++文件操作一一读取文件内容

包含头文件fstream,文件已经存在。

 1 #include <iostream>
 2 #include <fstream>
 3 #include <string>
 4 using namespace std;
 5 
 6 int main() {
 7   string str;
 8 //  size_t filesize;
 9 
10   ifstream file("test.txt", ios::in | ios::ate);
11 //  filesize = file.tellg();
12   
13 //  str.reserve(filesize);
14   
15   file.seekg(0);
16   while (!file.eof()) {
17     str += file.get();
18   }
19   cout << str;
20   
21   cout << endl;
22   system("pause");
23   return 0;
24 }
原文地址:https://www.cnblogs.com/imoon/p/2846065.html