C++檔案操作無法寫入的問題
阿新 • • 發佈:2019-01-06
今天測試C++檔案操作時,始終無法將資料寫入檔案中,百思不得其解,最後發現檔案操作完成後一定要將檔案關閉,即呼叫close()函式。
#include <iostream> #include <fstream> #include <string> using namespace std; int main(){ /*fstream file1; ofstream file2; file1.open("A.dat", ios::in); file2.open("abc.dat", ios::trunc); double temp,data[1600]; int index = 0; for(int i=0;i<1600;i++){ //file1 >> temp; //data[i] = temp; file2 <<i; //if (i % 40 == 0)file2 << "\n"; index++; }*/ ofstream file3; file3.open("b.txt", ios::trunc); for (int j = 0; j < 16; j++){ file3 <<j; file3 << " "; } file3.close(); cout << "succed"; int abc; cin >> abc; }