close
利用ofstream寫入檔案, ifstream讀入檔案
很明顯用標準函式庫比上一篇(Windows API WriteFile and ReadFile)使用Windows API語法簡單易懂多了!!!
1: #include "stdafx.h"
2: #include <fstream>
3: #include <iostream>
4:
5: using namespace std;
6:
7: int _tmain(int argc, _TCHAR* argv[])
8: {
9: // 寫入檔案
10: char msg[] = "我要寫入2";
11: ofstream out("test.txt");
12: cout << "寫入內容: " << endl;
13: cout << msg << endl;
14: out << msg;
15: out.close();
16: // 讀取檔案
17: ifstream in("test.txt");
18: char line[256];
19: in.getline(line, 256);
20: cout << "讀入內容: " << endl;
21: cout << line << endl;
22: system("pause");
23: return 0;
24: }
全站熱搜
留言列表