今天测试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;
}