开发者

Taking input for fstream::seekp and fstream::put

How would I go about taking input for seekp and put for use with hex? Something like this? Cin>>hex>>mychar;

How about printing the correct value in hex? Say at 0x16 the byte is FF or 255 unsigned 8bit(decimal?) when I do this

fstream mystream;
mystream.seekp(0x16);
long myvar;
myvar=mystream.tellp();
cout<<"Value at offset 0x16: "<<myvar;

It prints 22 im assuming since I opened the file in binary mode thats why its printing 22 as the value of FF in binary is like 2222222 etc.

Is there a correct data type for printing 开发者_运维技巧the value at a certain offset(I've tried long and char and int but it still yields 22.) or am I printing them wrong? I've searched on google but couldnt find any help in this regard however I did note that opening the file in binary is recommended for editing with hex.


There are a couple of problems with your code:

  • ostream::seekp sets the "put" pointer, i.e. the location at which one writes data, not the position at which one reads data. Use istream::seekg to set the "get" pointer
  • ostream::tellp doesn't perform any I/O. It merely reports the value of the 'put' pointer, it doesn't actually fetch the data stored there. You mean to use istream::get.

Your program is printing the decimal number 22, which is equal to the hexadecimal number 0x16. That is because seekp sets the put pointer to 0x16, and tellp merely reports the value of the put pointer -- 0x16!

Try this code:

fstream mystream;
mystream.seekg(0x16);
int myvar;
myvar=mystream.get();
cout<<"Value at offset 0x16: "<<myvar << "\n";
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜