Thread: Please help me with a code to read the value of a byte in a file.

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    22

    Question Please help me with a code to read the value of a byte in a file.

    Hello

    I'm new to c++ and to all forms of programming and am trying to get the source code for a program that prints out the hex value of any given byte in a file.

    I've gone through; this site, google, yahoo, cplusplus.com, C++ for dummies 5th Edition and, unbelievably, more. The answer just seems to elude me. No one says how to do this. I can only get the size of a file or read a txt file, but not the hex, or any other value of any given byte in a file. Thats the problem. Can ANYONE show me the source of a simple program that does this?

    I'm going to show you the closest I could get to this a majority may lose your lunch when you see it but you'll understand what a state I'm in with which I can only get the character of in a txt file and not the value of a byte of anyfile. I know I'm supposed to make the char unsigned but that stops me from compiling at all and give me errors.

    [tag]
    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    int x, y;
    int main () {
    char * buffer;
      Start:
      cout << "Enter the number of the byte you are looking for: ";
      cin >> x;
      if (x<1) {
      cout << "We begin counting at 0. Please try again.\n"; goto Start;}
      ifstream is;
      is.open ("Yo.bin", ios::in|ios::binary);
      is.seekg (x, ios::beg);
      is.read (buffer,1);
      is.close();
      cout << "\nThe hex value of byte number"<< x <<" is ";
      cout.write (buffer,1);
      cout << "\n\nDo you want to return to the start of this program?\n";
      cout << "Press 1 for YES or any other key for NO: ";
      cin >> y;
      if (y == 1) {goto Start;}
      if (y == !1) {goto End;}
      End:
      cout << "Goodbye!\n";
      delete[] buffer;
            return 0;}
    [/tag]
    Thanks for reading.
    Last edited by koxson; 05-07-2008 at 08:47 AM.

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Whether something is displayed as a hex value or a decimal value or a character is just a matter of output representation. The byte itself is still the same.
    To change the representation you can use I/O format flags. For example:
    Code:
    #include <iostream>
    int main()
    {
        char c = 'A';
        std::cout << std::showbase << std::hex << int(c);
    }
    In your code, buffer doesn't seem to be allocated, and since you are going to put just one character into it, may-be it could be declared as
    Code:
    char buffer[1] = {0};
    (May-be you could also read into a single char, but that would probably require ugly casts.)
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    You never allocate memory for buffer (and yet you delete it).
    Code:
    char *buffer = new char[2];
    Or, seeing as you're only reading a single char anyway,
    Code:
    char buffer;
    and get rid of the delete call.

    Gotos? In *my* C++ program?
    Code:
    int y = 0;
    while (!y)
    {
        // Do your thing
    }

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Is this a "find five problems"?

    You don't need that much code to make me loose my lunch, surely this can easily be written without goto's:
    Code:
      if (x<1) {
      cout << "We begin counting at 0. Please try again.\n"; goto Start;}
    Note that I'm not religiously against goto's, just think they should be avoided when there is a better, simpler alternative.
    Also, your error message doesn't seem to match exactly with the if-statement.

    Second problem:
    Code:
    char * buffer;
    This is a pointer to memory, but it's never assigned to any particular memory location, so it's going to crash, I suspect. Use a char variable, and pass the address to read.

    Third problem:
    Code:
    cout.write (buffer,1);
    This will display the ascii character (or whatever the local font representation of the data is), not the hex-value. To show a hex value, you need something like
    Code:
    cout << hex << buffer;

    Fourth problem:
    Code:
      cout << "Press 1 for YES or any other key for NO: ";
      cin >> y;
    This would fail if the user types in "a" for an answer.

    Problem five:
    Code:
      if (y == !1) {goto End;}
      End:
    So, not only do you use goto for ABSOLUTELY no reason, your if-statement is also not doing what you expect - not that you'd notice, because you end up at exactly the same place whether the if-statement is true or not, as there is no code between the goto and the lable end, but I challenge you to find a way to NOT print the message in this code:
    Code:
      if (y == !1) {goto End;}
         cout << "Got here" << endl;
      End:
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > delete[] buffer;
    You didn't allocate it, so you can't use it and you can't delete it.

    Also, use loop constructs in place of those goto statements.

    Also, there is no point in x and y being global.

    Edit: Meh, beaten *3
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Salem View Post
    > delete[] buffer;
    You didn't allocate it, so you can't use it and you can't delete it.

    Also, use loop constructs in place of those goto statements.

    Also, there is no point in x and y being global.

    Edit: Meh, beaten *3
    And I just realized it wasn't find five faults, but find eight faults...

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    you had the right idea, but a few details were off. consider the following:

    Code:
            int x;
            bool go=true;
            while(go)
            {
                    cout << "Enter the number of the byte you are looking for: ";
                    cin >> x;
                    if (x<0)
                    {
                            cout << "We begin counting at 0. Please try again.\n";
                    }
                    else
                    {
                            unsigned int y;
                            char buffer;
                            ifstream is;
                            is.open ("Yo.bin", ios::binary);
                            is.seekg (x, ios::beg);
                            is.read (&buffer,1);
                            is.close();
                            cout << "\nThe hex value of byte number "<< x <<" is " << hex << (int)buffer << endl;
                            cout << "\n\nDo you want to return to the start of this program?\n";
                            cout << "Press 0 for NO or any other key for YES: ";
                            cin >> y;
                            if(!y)
                            {
                                    go = false;
                            }
                    }
            }
            cout << "Goodbye!\n";
            return 0;
    good luck.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    cout << "\nThe hex value of byte number "<< x <<" is " << hex << (int)(unsigned char)buffer << endl;
    if you want to avoid having it sign-converted.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    could you not just cast it once to unsigned int?

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by m37h0d View Post
    could you not just cast it once to unsigned int?
    Perhaps, but I feel that it will still sign-extend to int first, then "unsign" that.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Registered User
    Join Date
    May 2008
    Posts
    22
    I wanted to come back to thank everyone on this thread for their help, especially m37h0d for putting it in terms the layman to the world of c++ can understand.

    Thanks m37h0d!

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    Quote Originally Posted by koxson View Post
    I wanted to come back to thank everyone on this thread for their help, especially m37h0d for putting it in terms the layman to the world of c++ can understand.

    Thanks m37h0d!
    i did?

    yay me!

    making the code as clean and simple as possible should always be a goal imo. i didn't write that in any fashion that was supposed to be "for the layman"; i just tried to make it as clear and simple as possible, which as far as i'm concerned is always good programming!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  3. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM