所以我对此有点困惑,我无法准确地弄清楚我的文件输入在这里发生了什么。我应该指出,我刚刚将它移植到了 Windows,它确实可以在 Mac OS X 上正常运行并按预期运行。
基本上,我的问题是我的 ifstream 似乎在读取输入文件的尾部时复制了两次。例如,如果我的输入是:
Daisy, Daisy, give me your answer, do,
I'm half crazy all for the love of you.
It won't be a stylish marriage,
I can't afford a carriage,
But you'd look sweet upon the seat
Of a bicycle built for two.
ifstream 将按如下方式读取:
Daisy, Daisy, give me your answer, do,
I'm half crazy all for the love of you.
It won't be a stylish marriage,
I can't afford a carriage,
But you'd look sweet upon the seat
Of a bicycle built for two.
I can't afford a carriage,
But you'd look sweet upon the seat
Of a bicycle built for two.
这是有问题的代码:
std::ifstream initialResults(inputFileLocation.toStdString().c_str());
std::string fileInMemory;
initialResults.seekg(0,initialResults.end);
fileInMemory.resize(initialResults.tellg());
initialResults.seekg(0,initialResults.beg);
initialResults.read(&fileInMemory[0],fileInMemory.size());
initialResults.close
//Printing here, the file in memory already contains the duplicate entries
std::cout << fileInMemory << "\n";
我完全不确定是什么导致了这种行为。我不太熟悉在 Windows 上与 Mac 相对的开发,考虑到程序是相同的,但会产生这些不同的结果,我将冒险说这是 Window 特有的。也许结束和乞求的函数调用有所不同?但我无法想象为什么它会重新复制文件的尾部。是的,我检查了文件,里面没有重复的条目。