C++向txt文件写入数据

1.程序

#include 
#include 
#include 
#include 

using namespace std;

int main(){
	vector<int> forwrite; 
	for(int t = 0; t < 10; t++){
		forwrite.push_back(t);
	}
	ofstream OutFile("./myfile.txt");
	for(int t =0; t < 10; t++){
		char cw = forwrite[t] + '0';
		//或者可以用下面这行方法转,当将较大的数转换成字符串时,就可以用这一句了,如果仅转为char可以继续用上面那句
		//OutFile<
		OutFile<< cw <<" ";	
	}
	OutFile<<"\n";
	OutFile.close();
	return 0;
}

2.结果

0 1 2 3 4 5 6 7 8 9 

你可能感兴趣的:(C++)