c++ 寫入字符串到文件

直接貼代碼:

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

using namespace std;


int main()
{
    vector<string> files;
    for (int i = 0; i < 10; i++)
    {
        stringstream str;
        str << i;
        files.push_back(str.str());
    }

    ofstream outfile;
    outfile.open("files1.txt", ios_base::out | ios_base::trunc);  //刪除文件重寫
    for (int i = 0; i < files.size(); i++)
    {
        stringstream ss;
        ss << files[i];
        outfile << ss.str() << endl;
    }
    outfile.close();

    return 0;
}

 

語法鏈接:

https://www.cnblogs.com/mupiaomiao/p/4730757.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章