Appending content of text file to another file in C++

我与影子孤独终老i 提交于 2019-12-02 01:53:46
jrd1

I can only speak for opening a file and appending it to another file:

std::ifstream ifile("first_file.txt");
std::ofstream ofile("second_file.txt", std::ios::app);

//check to see that the input file exists:
if (!ifile.is_open()) {
    //file not open (i.e. not found, access denied, etc). Print an error message or do something else...
}
//check to see that the output file exists:
else if (!ofile.is_open()) {
    //file not open (i.e. not created, access denied, etc). Print an error message or do something else...
}
else {
    ofile << ifile.rdbuf();
    //then add more lines to the file if need be...
}

REFERENCES:

http://www.cplusplus.com/doc/tutorial/files/

https://stackoverflow.com/a/10195497/866930

std::ifstream in("in.txt");
std::ofstream out("out.txt", std::ios_base::out | std::ios_base::app);

for (std::string str; std::getline(in, str); )
{
    out << str;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!