读书人

c++ fstream write和read的有关问题

发布时间: 2012-02-16 21:30:36 作者: rapoo

c++ fstream write和read的问题
#include <fstream>
#include <iostream>
using namespace std;

const char const *name = "temp.bin ";

int main()
{
fstream file;
char str[10] = "123456789 ";

file.open(name, ios::in|ios::out|ios::binary);

file.write(str, 9);
cout < < str < < endl;

file.read(str, 5);
cout < < str < < endl;


file.close();

system( "pause ");
return 0;
}
---------------------------
代码如上, 但是结果却是

123456789
屯屯?789

不知道是怎么个情况,望知道的指点下 ,谢了


[解决办法]
file.seekg( 0, ios_base::beg );

file.read(str, 5);


[解决办法]
file.write(str, 9);
cout < < str < < endl;el;
-------------------
文件的指针在文件尾

file.read(str, 5);
cout < < str < < endl;dl;
-------------------
从文件尾读,当然写不出你想要的

像一楼那样改就可以了
[解决办法]
文件的指针在文件尾

读书人网 >C++

热点推荐