頭文件
ofstream -- 向文件寫內容
實現代碼
#include <vector>
#include <string>
#include <fstream>
#include <iostream>
using std::ofstream;
void CMFCyaraguiDlg::C2ToYaraText(vector<string> result)
{
ofstream outfile; //輸出流
int cost[10][10];
outfile.open("G:\\C++ project\\Read\\result.txt", ios::app); //每次寫都定位的文件結尾,不會丟失原來的內容,用out則會丟失原來的內容
if(!outfile.is_open ())
{ cout << "Open file failure" << endl; }
for (int i = 0; i != 10; ++i)
{
for (int j = 0; j != 10; ++j)
{
outfile << i << "\t" << j << "\t" << cost[i][j] << endl; //在result.txt中寫入結果
}
}
outfile.close();
}
參考
C++讀寫TXT文件中的string或者int型數據以及string流的用法
https://www.cnblogs.com/1242118789lr/p/6885691.html
