C++寫文件


頭文件

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM