C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 05-07-2008, 08:45 AM   #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.
koxson is offline   Reply With Quote
Old 05-07-2008, 09:14 AM   #2
The larch
 
Join Date: May 2006
Posts: 3,086
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.

Quote:
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).
anon is offline   Reply With Quote
Old 05-07-2008, 09:18 AM   #3
and the Hat of Ass
 
Join Date: Dec 2007
Posts: 731
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
}
rags_to_riches is offline   Reply With Quote
Old 05-07-2008, 09:19 AM   #4
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
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.
matsp is offline   Reply With Quote
Old 05-07-2008, 09:21 AM   #5
and the hat of vanishing
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,214
> 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.
Up to 8Mb PlusNet broadband from only £5.99 a month!
Salem is offline   Reply With Quote
Old 05-07-2008, 09:25 AM   #6
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
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.
matsp is offline   Reply With Quote
Old 05-07-2008, 09:37 AM   #7
3735928559
 
Join Date: Mar 2008
Posts: 662
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.
m37h0d is offline   Reply With Quote
Old 05-07-2008, 09:41 AM   #8
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
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.
matsp is offline   Reply With Quote
Old 05-07-2008, 09:46 AM   #9
3735928559
 
Join Date: Mar 2008
Posts: 662
could you not just cast it once to unsigned int?
m37h0d is offline   Reply With Quote
Old 05-07-2008, 09:48 AM   #10
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
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.
matsp is offline   Reply With Quote
Old 05-29-2008, 10:20 AM   #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!
koxson is offline   Reply With Quote
Old 05-29-2008, 10:56 AM   #12
Mysterious C++ User
 
Join Date: Oct 2007
Posts: 14,099
http://cpwiki.sourceforge.net/Common...llocating_them
__________________
Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System
I dedicated my life to helping others. This is only a small sample of what they said:
"Thanks Elysia. You're a programming master! How the hell do you know every thing?"
Quoted... at least once.
Quote:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Old 05-29-2008, 11:36 AM   #13
3735928559
 
Join Date: Mar 2008
Posts: 662
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!
m37h0d is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
A development process Noir C Programming 30 10-28-2009 04:24 AM
gcc link external library spank C Programming 6 08-08-2007 03:44 PM
brace-enclosed error jdc18 C++ Programming 53 05-03-2007 05:49 PM
airport Log program using 3D linked List : problem reading from file gemini_shooter C Programming 3 03-04-2005 02:46 PM


All times are GMT -6. The time now is 01:11 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22