首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

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

2012-02-16 
c++ fstreamwrite和read的问题#includefstream#includeiostreamusingnamespacestdconstcharconst*nam

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;
-------------------
从文件尾读,当然写不出你想要的

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

热点排行