problem with hex file editing

I'm kinda new to C++ and I've started to build a program.
the program need to edit a hex variable at a specific file. everything seems to work well except when the program save the variable, something goes wrong.

what i do i use this line
file << hex << var;

what happens is that the user enter a decimal number, and i want to save the decimal number in its hex value, for example if the user enter 255, ff will be saved in the file

the problem is it save the var in a wrong way, if the var decimal value is 255, the hex value is ff, and it will save it as 66 66, which is the hex value of the characters ff

so the problem is that it saves the hex value of the variable characters, how do i make it save only the variable? so if the decimal value is 255, it will save the hex value of the integer 255? so ff will be saved in the file and not 66 66?

so its translating the hex into text! how do i save the hex value without translating it as text to hex?

examples and tutorials how to do it will help a lot, and please keep it simple cause I'm pretty new
i hope you can understand cause its hard to me to explain
Last edited on
Which is the type of 'var' ? It should work
http://www.cplusplus.com/reference/iostream/manipulators/hex/
well, this is what I've did, but it doesn't work
could you give an example about how to do it correctly?

i've edited my first post and added this line which explain the problem:
so its translating the hex into text! how do i save the hex value without translating it as text to hex?
Last edited on
i think i understand the problem, it save it as a string? cause the hex in the file is the text i put in, how do i edit the value of the hex integer?
Can you answer my question, which is the type of 'var' ?
what do you mean which type? an integer i guess, its decimal that i want to save of the file as hex
Last edited on
'Hex' and 'decimal' and the like only exist as textual data. A number is a number is a number, no matter how you write it.

Thus, FF hexadecimal == 255 decimal == 377 octal == 11111111 binary == 2222 trinary == etc. The number is the same. The way it is written is different.

So, if you want to write a single byte, you must use the unformatted output operations: ostream::put() or ostream::write().

So, if you wanted to modify the sixth byte in a file to have the value 0x41 (in ASCII that's a capital 'A'):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <fstream>
#include <iostream>
#include <string>
using namespace std;

void create_file( const string& filename )
  {
  cout << "Creating '" << filename << "'..." << flush;
  ofstream outf( filename.c_str() );

  outf << "Have a nice day!\n";
  outf << "See you later, handsome!\n";

  outf.close();
  cout << "\b\b\b   \nDone.\n\n";
  }

void modify_file( const string& filename )
  {
  cout << "Modifying '" << filename << "'..." << flush;
  fstream f(
    filename.c_str(),
    ios::in | ios::out | ios::binary
    );

  f.seekp( 5, ios::beg );
  f.put( 0x41 );

  f.close();
  cout << "\b\b\b   \nDone.\n\n";
  }

void display_file( const string& filename )
  {
  string   s;
  ifstream f( filename.c_str() );
  unsigned n = 0;

  cout << "Contents of '" << filename << "'\n";

  while (getline( f, s ))
    cout << ++n << ": " << s << endl;

  f.close();
  cout << endl << endl;
  }

int main()
  {
  const char* filename = "hello.txt";

  create_file ( filename );
  display_file( filename );
  modify_file ( filename );
  display_file( filename );

  return 0;
  }

Some notes:

Line 23 is optional. I opened the file in binary mode. This wasn't necessary in this case, since the file was a text file. However, when dealing with binary data it is usually a good idea to open files in binary mode.

The unformatted I/O functions take char as argument... which may be signed or unsigned depending on your implementation -- so you should always cast to the appropriate type. It will work correctly either way.

Well, that's all the over-the-top example you're going to get from me... Hope this helps.
Thanks a lot Duoas! you solved the problem! so i had to use the put()
thanks again
Topic archived. No new replies allowed.