473,785 Members | 2,990 Online
Bytes | Software Development & Data Engineering Community
+ 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 2346
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_t ype size;
ifstream file;

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

file.open ("s0455_re.dat" , ios::in|ios::bi nary|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
3077
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: --cut here-- typedef struct pkg_ { short int info; char* data;
6
3796
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 trying to read a flatfile. In COBOL, my simple file layout and READ statement would look like below. Question: what is the standard, simple coding convention for reading in a flatfile - one record at a time?? SCANF does not work because of...
50
5015
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 -- the character input is a different matter, at least if I want to remain within the bounds of the standard library.
7
6063
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 populate a series of structures of specified variable composition. I have the structures created OK, but actually reading the files is giving me an error. Can I ask a simple question to start with: I'm trying to read the file using the...
30
4588
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
5274
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
9885
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 should loop 15,870,080 times. The code is: newprogram.cpp =============
3
2838
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 (!file.eof ()) { do {
6
11921
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 like this: S~5BBBBBBBBBBBBBBB5BBBBBBBB5B31BB4BB2B5BB4BBBE S is the start of the line, ~ indicates a good read, B is a blank reading, and the numbers 1-6 correspond to a location on the device for a line. So there are six positions on the line...
6
3531
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 to an old program that I wrote in delphi and it's a good opportunity to start with c++.
0
9480
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10327
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10092
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9950
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7499
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6740
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4053
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 we have to send another system
2
3647
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2879
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.