0
点赞
收藏
分享

微信扫一扫

将数据保存为txt c++

菜头粿子园 2022-05-05 阅读 144
c++后端

将数据保存为txt c++

采用c++中的fstream对数据进行写入操作,保存为txt格式,代码编写如下,亲测可用:

#include <iostream>
#include <fstream>
#include <string>
#include <vector>

using namespace std;

int size = 9;
unsigned long txtCount = 2; 
int main() {
string txt_path = "out-data/"; 
string txt_name = txt_path + to_string(txtCount) + ".txt";
ofstream output_stream;  //(txt_name)
output_stream.open(txt_name);
output_stream << "# index " << "\n";
for(int i=0; i<size; i++){
    output_stream << std::to_string(i);
	output_stream << " ";
    output_stream << endl; 
}

//txtCount = txtCount + 1;
output_stream.close();
return 0;
}

结果如下图所示:

在这里插入图片描述

举报

相关推荐

0 条评论