how can i copy line by line from an old file to a new file?

need help.
You can do it like this:

1
2
3
4
5
std::ifstream ifs("input.txt");
std::ofstream ofs("output.txt");
std::string buffer;
while(std::getline(ifs, buffer)) ofs << buffer << std::endl; /* getline() doesn't read the newline,
                                                                so we add it back to the output */

Remember to include <string> and <fstream>. Also remember to do the error handling. If you need to know more, read this: http://www.cplusplus.com/doc/tutorial/files/
Last edited on
Topic archived. No new replies allowed.