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.