c ++以相反的顺序逐行传输文件

时间:2013-02-25 03:51:23

标签: c++

我有这个代码,它应该以相反的顺序逐行传输文件到另一个文件中。但它不起作用。也许我忘了添加一些东西:

while(cnvFile.good()) {
    getline(cnvFile, cnvPerLine);

    reverseFile << cnvPerLine;
    reverseFile.seekp(0, ios::beg);
}

1 个答案:

答案 0 :(得分:1)

当您寻求开始并尝试编写时,您不是插入数据,而是覆盖数据。一个简单的(尽管可能远非最佳)解决方案将是:

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...