C++写入txt

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://www.cnblogs.com/Bearoom/p/11721766.html