473,473 Members | 2,292 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Reading Binary

14 New Member
Hi.

I am trying to read .dat files which is in binary using visual C++ 6.0. I want to display the first 100 char (in hex). But instead some weird char are being display. I opened the file through hex editor and this is what i am supposed to get: 2EFACCF69FFC84...

Can anyone help me in this? Thanks in advance!
Expand|Select|Wrap|Line Numbers
  1. char *memblock;
  2. ifstream::pos_type size;
  3. ifstream file;
  4.  
  5. int main ()
  6. {
  7.     int size, i;
  8.     char n;
  9.  
  10.     file.open ("s0455_re.dat", ios::in|ios::binary|ios::ate);
  11.  
  12.     if (file.is_open())
  13.     {
  14.         cout << "Able to open file!";    
  15.                                // Able to get the size since it is at the end
  16.         size = (int) file.tellg(); 
  17.  
  18.         // allocate memory
  19.         memblock = new char [size]; 
  20.  
  21.         file.seekg (0, ios::beg);
  22.  
  23.         for (i=0; i<100; i++)
  24.         {    
  25.             file.seekg (0, ios::cur);
  26.             file.read (memblock, 1);
  27.             n = *memblock;
  28.             cout << n;
  29.         }
  30.  
  31.         file.close();
  32.     }
  33.     else cout << "Unable to open file";
  34.     return 0;
  35. }
  36.  
Jan 26 '07 #1
7 2330
willakawill
1,646 Top Contributor
Hi.

I am trying to read .dat files which is in binary using visual C++ 6.0. I want to display the first 100 char (in hex). But instead some weird char are being display. I opened the file through hex editor and this is what i am supposed to get: 2EFACCF69FFC84...

Can anyone help me in this? Thanks in advance!

char *memblock;
ifstream::pos_type size;
ifstream file;

int main ()
{
int size, i;
char n;

file.open ("s0455_re.dat", ios::in|ios::binary|ios::ate);

if (file.is_open())
{
cout << "Able to open file!";
// Able to get the size since it is at the end
size = (int) file.tellg();

// allocate memory
memblock = new char [size];

file.seekg (0, ios::beg);

for (i=0; i<100; i++)
{
file.seekg (0, ios::cur);
file.read (memblock, 1);
n = *memblock;
cout << n;
}

file.close();
}
else cout << "Unable to open file";
return 0;
}
Hi. If you compare your code to this you may discover how to make it work:
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <fstream>
  3. using namespace std;
  4.  
  5. int main () {
  6.   int length;
  7.   char * buffer;
  8.  
  9.   ifstream is;
  10.   is.open ("test.txt", ios::binary );
  11.  
  12.   // get length of file:
  13.   is.seekg (0, ios::end);
  14.   length = is.tellg();
  15.   is.seekg (0, ios::beg);
  16.  
  17.   // allocate memory:
  18.   buffer = new char [length];
  19.  
  20.   // read data as a block:
  21.   is.read (buffer,length);
  22.  
  23.   is.close();
  24.  
  25.   cout.write (buffer,length);
  26.  
  27.   return 0;
  28. }
Jan 26 '07 #2
UnknownBlue
14 New Member
Hi.

Thanks for replying.
I have tried that but it gives an output of two lines like =.
Its not in terms of hex.
Jan 26 '07 #3
willakawill
1,646 Top Contributor
Hi.

Thanks for replying.
I have tried that but it gives an output of two lines like =.
Its not in terms of hex.
Adjusted
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <fstream>
  3. using namespace std;
  4.  
  5. int main () {
  6.   int length;
  7.   char * buffer;
  8.   char output[2];
  9.   int lp;
  10.  
  11.   ifstream is;
  12.   is.open ("test.txt", ios::binary );
  13.  
  14.   // get length of file:
  15.   is.seekg (0, ios::end);
  16.   length = is.tellg();
  17.   is.seekg (0, ios::beg);
  18.  
  19.   // allocate memory:
  20.   buffer = new char [length];
  21.  
  22.   // read data as a block:
  23.   is.read (buffer,length);
  24.  
  25.   is.close();
  26.  
  27.   if (length > 100) length = 100;
  28.  
  29.   for (lp = 0; lp < length; lp++) {
  30.     sprintf(output, "%X", atoi(buffer[lp]));
  31.     cout << output;
  32.   }
  33.   return 0;
  34. }
Jan 26 '07 #4
willakawill
1,646 Top Contributor
That last part will not work.
this piece of code will print out in hex so maybe you can adapt it
Expand|Select|Wrap|Line Numbers
  1. char ar[30] = {0};
  2. char buffer[20] = {0};
  3. sprintf(ar, "%d", 123456789);
  4. sprintf(buffer, "%X", atol(ar));
  5. cout << buffer;
If you read the file 4 chars at a time into a long variable you could achieve what you need rather than into a char array.
Jan 26 '07 #5
UnknownBlue
14 New Member
It sort of work!
Thanks!
Jan 29 '07 #6
horace1
1,510 Recognized Expert Top Contributor
with a few minor modifications your original program would work, e.g. in particular cast the byte read as a char to an int and use the io manipulator to print it as hex
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <fstream>
  3. using namespace std;
  4.  
  5. char memblock;  // ** remove *
  6. ifstream::pos_type size;
  7. ifstream file;
  8.  
  9. int main ()
  10. {
  11. int size, i;
  12. char n;
  13.  
  14. file.open ("s0455_re.dat", ios::in|ios::binary|ios::ate);
  15.  
  16. if (file.is_open())
  17. {
  18. cout << "Able to open file!";
  19. // Able to get the size since it is at the end
  20. size = (int) file.tellg();
  21.  
  22. // allocate memory
  23. //memblock = new char [size];  // ** not needed
  24.  
  25. file.seekg (0, ios::beg);
  26.  
  27. for (i=0; i<100; i++)
  28. {
  29. file.seekg (0, ios::cur);
  30. file.read (&memblock, 1);  // ** added &
  31. //n = *memblock;  // ** remove
  32. cout << hex << (int) memblock;  // ** output as hex
  33. }
  34.  
  35. file.close();
  36. }
  37. else cout << "Unable to open file";
  38. cin.get();
  39. return 0;
  40. }
  41.  
Jan 29 '07 #7
LSB
8 New Member
You sure may want to make some minor changes:

Expand|Select|Wrap|Line Numbers
  1. int size100;
  2. ……………..
  3. size100 = (size < 100) ? size : 100;
  4. ……………..
  5. for (i =0; i<size100; i++)
  6. ..................
Jan 29 '07 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

20
by: ishmael4 | last post by:
hello everyone! i have a problem with reading from binary file. i was googling and searching, but i just cant understand, why isnt this code working. i could use any help. here's the source code:...
6
by: KevinD | last post by:
assumption: I am new to C and old to COBOL I have been reading a lot (self teaching) but something is not sinking in with respect to reading a simple file - one record at a time. Using C, I am...
50
by: Michael Mair | last post by:
Cheerio, I would appreciate opinions on the following: Given the task to read a _complete_ text file into a string: What is the "best" way to do it? Handling the buffer is not the problem...
7
by: John Dann | last post by:
I'm trying to read some binary data from a file created by another program. I know the binary file format but can't change or control the format. The binary data is organised such that it should...
30
by: siliconwafer | last post by:
Hi All, I want to know tht how can one Stop reading a file in C (e.g a Hex file)with no 'EOF'?
6
by: arne.muller | last post by:
Hello, I've come across some problems reading strucutres from binary files. Basically I've some strutures typedef struct { int i; double x; int n; double *mz;
9
by: Use*n*x | last post by:
Hello, I have a binary file (image file) and am reading 4-bytes at a time. The File size is 63,480,320 bytes. My assumption is that if I loop through this file reading 4 bytes at a time, I...
3
by: The Cool Giraffe | last post by:
Regarding the following code i have a problem. void read () { fstream file; ios::open_mode opMode = ios::in; file.open ("some.txt", opMode); char *ch = new char; vector <charv; while...
6
by: John Wright | last post by:
I am trying to read the data from a device on a serial port. I connect just fine and can receive data fine in text mode but not in binary mode. In text mode the data from the device comes in...
6
by: efrenba | last post by:
Hi, I came from delphi world and now I'm doing my first steps in C++. I'm using C++builder because its ide is like delphi although I'm trying to avoid the vcl. I need to insert new features...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.