我有这样的代码,它应该以相反的顺序逐行将一个文件传输到另一个文件中。但是它不起作用。也许我忘了添加一些东西:
while(cnvFile.good()) {
getline(cnvFile, cnvPerLine);
reverseFile << cnvPerLine;
reverseFile.seekp(0, ios::beg);
}
当您寻找开始并尝试写入时,您不是插入数据,而是覆盖的数据。一个简单的(尽管可能远不是最佳的(解决方案是这样的:
std::string reversedContents
while (getline(inFile, line)) {
// This actually *appends* to the beginning, not overwriting
reversedContents = line + "n" + reversedContents; // manually add line breaks back in
}
// now write reversedContents to a file...