5

I have a simple question that is confusing me.

Goal: I want to read a given byte from a file (say the first byte) and make int x with the ASCII value of that byte. So, for example, if the byte/character is 'a', I want x to be 97 (= 61 in hex). I have the following reading the first byte of the file example.txt:

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main(){
  unsigned int x;
  unsigned char b;
  ifstream myFile ("example.txt", ios::out | ios::binary);
  myFile.seekg (0, ios::beg);
  myFile >> b;
  x = (unsigned int)b;
  cout << hex << x;
  return b;
}

Problem: If the first byte is represented by 08, then indeed I get an output of 8. But if the byte is represented by 09, then I get 0. I have noticed that I seem to get the following byte unless that byte is also 09. I don't know if my problem is only when the byte is represented in ASCII by 09.

Question: So how to I read say the first (or third or whatever) byte from a file and make an int with the ASCII value of that byte?

(I am on Windows XP)

Thomas
  • 1,085
  • 5
  • 19
  • 33

3 Answers3

3

This should fix it.

 myFile >> noskipws >> b;
Mark H
  • 13,797
  • 4
  • 31
  • 45
3

Couple of suggestions:

  • Check if the file has actually been opened. If it wasn't, ensure the file is in the current dir, or supply the full path.
  • You probably want ios::in (not ios::out).
  • Use noskipws unless you actually want to skip whitespaces.
  • What's the purpose of returning the character? The convention is to interpret a non-zero exit code as a failure (though admittedly, there is no strict standard on this).

The following program reads the 4th character and prints its HEX value just fine for me:

#include <iostream>
#include <fstream>
#include <stdlib.h>

using namespace std;

int main() {

    ifstream myFile("<path>\\example.txt", ios::in | ios::binary);

    if (myFile) {

        unsigned char b;
        myFile.seekg(3) >> noskipws >> b;

        if (myFile) { // File was long enough?
            unsigned int x = b;
            cout << hex << x;
            return EXIT_SUCCESS;
        }

    }

    return EXIT_FAILURE;

}

(Replace <path> with actual path.)

Community
  • 1
  • 1
Branko Dimitrijevic
  • 50,809
  • 10
  • 93
  • 167
  • Thanks for the suggestions. The noskipws solution worked for me. Returning the character was just a mistake from when I was playing around with things. Why should I use ios::in instead of ios::out when I want to read from the file? (you might already have realized that I am new to C++) – Thomas Mar 03 '12 at 03:10
  • 1
    @ThomasM Because you want to "input" from the file, not "output" to it. – Branko Dimitrijevic Mar 03 '12 at 03:17
  • 1
    @ThomasM An undefined behavior can crash your program, or print the works of Shakespeare, or polish your shoes ;) But it may also just work! That doesn't mean you should rely on it working under different circumstances (e.g. different compiler or platform). – Branko Dimitrijevic Mar 03 '12 at 04:12
2

Try reading using ifstream::read instead of operator>>. This has worked for me:

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main(){
  unsigned int x;
  unsigned char b;
  ifstream myFile ("example.txt", ios::out | ios::binary);
  myFile.seekg (0, ios::beg);
  myFile.read(reinterpret_cast<char*>(&b), sizeof(b));
  x = (unsigned int)b;
  cout << hex << x;
  return b;
}
mfontanini
  • 21,410
  • 4
  • 65
  • 73