11

As the title says, how do you read hex values using fstream?

i have this code: (let's say we have "FF" in the file.)

fstream infile;
infile.open(filename, fstream::in|fstream::out|fstream::app);

int a;
infile >> std::hex;
infile >> a;
cout << hex << a;

but this does not give me any output instead of ff. I know there is a fscanf(fp, "%x", val) but I am curious is there any way to do this using stream library.

UPDATE:

My code was right all along, it turns out my error was I couldn't read "FFF" and put it in variable a,b,c like this

while (infile >> hex >> a >> b >> c)
  {
    cout << hex << a << b << c << "\n";
  }

Can somebody help me with this? do I have to separate every HEX values i want to read with space? because infile >> hex >> setw(1) doesn't work..

ardiyu07
  • 1,790
  • 2
  • 17
  • 29
  • 1
    This has nothing to do with hex values. Your error lies elsewhere. – etarion Feb 18 '11 at 11:39
  • before asking to here, search on google. I am sure you will find answer for this trivial question –  Feb 18 '11 at 11:46
  • @gcc I searched on google but it didn't work for my case.. – ardiyu07 Feb 18 '11 at 11:49
  • @etarion you were right, but I have another question please help me :| – ardiyu07 Feb 18 '11 at 11:50
  • 4
    @gcc: Sometimes it's difficult to express your query in that "Text box". Therefore, it's desirable to ask it in here. Also, you may end up getting broken links or incorrect information. This site is backed up by active community. So an answer in here is worth more even if the same could have been found using Google. – Shamim Hafiz - MSFT Feb 18 '11 at 11:51
  • @ardiyu07, search "overloading" ," how to use assignment operator with comma"," more info or example on "hex" ".search Something similar to my answer. @Gunner, you say "no give any attempt to discover answered question replayed in somewhere else",I think –  Feb 18 '11 at 12:01

4 Answers4

12

You can use the hex modifier

int n;
cin >> hex >> n;
Bernd Elkemann
  • 23,242
  • 4
  • 37
  • 66
7

This works:

int main()
{
    const char *filename = "blah.txt";
    ifstream infile(filename, fstream::in);

    unsigned int a;
    infile >> hex >> a;
    cout << hex << a;
}
Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
6

You have to chain std::hex when reading, the same way you chain it for writing :

infile >> std::hex >> a;
Nekresh
  • 2,948
  • 23
  • 28
  • thanks for the answer! I have tried that but also it does not work :( – ardiyu07 Feb 18 '11 at 11:31
  • 1
    @ardiyu: It does work. http://ideone.com/qs2Z5 Also using std::hex on a seperate line, like you did, works. Your error lies elsewhere. – etarion Feb 18 '11 at 11:34
  • @etarion @Nekresh You guys right it worked! but I got another question please see the update and help me :| – ardiyu07 Feb 18 '11 at 11:47
-2

Also make sure that your input file is written using a Hex editor and not a regular text editor. Otherwise a file foo.txt containing a character 'a' will be read as 0x61 and printed as 0x61 instead of 0xa. A nice Hex editor for linux is "Bless".

Than21
  • 321
  • 1
  • 2
  • 10
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/low-quality-posts/15865427) – Christoph Apr 17 '17 at 18:49
  • My point is to use a Hex editor - doesn't matter which one. Therefore, the link is optional in my answer. It is only a suggestion. I understand your point though. Thanks. – Than21 Apr 17 '17 at 19:52