C++写入txt

180it 2020-10-08 PM 1452℃ 0条

C++写txt的时候可以用到std::ofstream来实现。

代码:

#include <stdlib.h>
#include <fstream>
#include <string>
#include <iostream>
 
int main(int argc, char* argv[])
{
    std::ifstream fIn("str.txt");
    std::ofstream fOut("str2.txt");
    if (!fIn)
    {
        std::cout << "Open input file faild." << std::endl;
    }
    if (!fOut)
    {
        std::cout << "Open output file faild." << std::endl;
    }
    std::string str;
    while (std::getline(fIn, str))
    {
        std::cout << str << std::endl;
        fOut << str << std::endl;
    }
    fIn.close();
    fOut.close();
    system("pause");
    return 0;
}

https://blog.csdn.net/yz2zcx/article/details/100587716

支付宝打赏支付宝打赏 微信打赏微信打赏

如果文章或资源对您有帮助,欢迎打赏作者。一路走来,感谢有您!

标签: none

C++写入txt