ifstream读取读取整个流的随机字符

时间:2015-07-28 21:30:50

标签: c++

我正在尝试从列表5.1 here实现该功能 但是当从文件中读取复制到缓冲区时,我只得到整个数组的相同字符(Í),其中string.txt是前一个链接内容的复制和粘贴。 enter image description here  这是我的代码:

Prob Id Problem Date    Affected Id Aff Date Range
3       2013-03-30      4           2013-01-01 - 2015-02-10
3       2013-03-30      5           2013-01-01 - 2999-01-01

我希望你能帮我看看我做错了什么,谢谢!

1 个答案:

答案 0 :(得分:0)

You are setting myFile's position to ios_base::end with seekg:

myFile.seekg(0, ios::end);

Then trying to read from it:

myFile.read(buffer, block_size);

Clearly no data will be read since myFile is already at ios_base::end. And you'll be reading whatever uninitialized data that was already in buffer

What you probably intended to do was to set your myFile position back to the beginning by doing this before reading:

myFile.seekg(0, ios::beg);