Reputation: 1
Recently I've been working on some scripts, and today I encountered a little problem, I would like to interchange the values of 2 offsets of a binary file. Like interchanging the value of the position 0x00 with the value of 0x01, I tried with file.seekp(0x00) and then get the value, hold it in a char, and later to insert the value on the position 0x01 I used file.seekp(0x01) and then file.put(char), but the main problem is that it's not modifying the value of 0x01, just adds the char to it.
Is there any way I could modify it's value ?
before attempting to interchange the first two offsets values :
after attempting to interchange the first two offsets values :
And basically, what I need is to interchange them, not to add to the second offset the first's value .
The code I used :
char c[1];
fstream file(path, ios::in | ios::out | ios::binary );
file.seekp(0x00);
c[0] = file.get();
file.seekp(0x01);
c[1] = file.get();
file.put(c[0]);
file.seekp(0x00);
file.put(c[1]);
Upvotes: 0
Views: 157
Reputation: 50846
c
should be of size 2 and get
/put
move the file cursor leading to the issue.
Here is the corrected code:
char c[2];
fstream file("test", ios::in | ios::out | ios::binary);
file.seekp(0x00);
c[0] = file.get();
c[1] = file.get();
file.seekp(0x00);
file.put(c[1]);
file.put(c[0]);
Upvotes: 1