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;
-
}
-
7 2346
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: - #include <iostream>
-
#include <fstream>
-
using namespace std;
-
-
int main () {
-
int length;
-
char * buffer;
-
-
ifstream is;
-
is.open ("test.txt", ios::binary );
-
-
// get length of file:
-
is.seekg (0, ios::end);
-
length = is.tellg();
-
is.seekg (0, ios::beg);
-
-
// allocate memory:
-
buffer = new char [length];
-
-
// read data as a block:
-
is.read (buffer,length);
-
-
is.close();
-
-
cout.write (buffer,length);
-
-
return 0;
-
}
Hi.
Thanks for replying.
I have tried that but it gives an output of two lines like =.
Its not in terms of hex.
Hi.
Thanks for replying.
I have tried that but it gives an output of two lines like =.
Its not in terms of hex.
Adjusted - #include <iostream>
-
#include <fstream>
-
using namespace std;
-
-
int main () {
-
int length;
-
char * buffer;
-
char output[2];
-
int lp;
-
-
ifstream is;
-
is.open ("test.txt", ios::binary );
-
-
// get length of file:
-
is.seekg (0, ios::end);
-
length = is.tellg();
-
is.seekg (0, ios::beg);
-
-
// allocate memory:
-
buffer = new char [length];
-
-
// read data as a block:
-
is.read (buffer,length);
-
-
is.close();
-
-
if (length > 100) length = 100;
-
-
for (lp = 0; lp < length; lp++) {
-
sprintf(output, "%X", atoi(buffer[lp]));
-
cout << output;
-
}
-
return 0;
-
}
That last part will not work.
this piece of code will print out in hex so maybe you can adapt it - char ar[30] = {0};
-
char buffer[20] = {0};
-
sprintf(ar, "%d", 123456789);
-
sprintf(buffer, "%X", atol(ar));
-
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.
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 -
#include <iostream>
-
#include <fstream>
-
using namespace std;
-
-
char memblock; // ** remove *
-
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]; // ** not needed
-
-
file.seekg (0, ios::beg);
-
-
for (i=0; i<100; i++)
-
{
-
file.seekg (0, ios::cur);
-
file.read (&memblock, 1); // ** added &
-
//n = *memblock; // ** remove
-
cout << hex << (int) memblock; // ** output as hex
-
}
-
-
file.close();
-
}
-
else cout << "Unable to open file";
-
cin.get();
-
return 0;
-
}
-
LSB 8
New Member
You sure may want to make some minor changes: -
int size100;
-
……………..
-
size100 = (size < 100) ? size : 100;
-
……………..
-
for (i =0; i<size100; i++)
-
..................
Sign in to post your reply or Sign up for a free account.
Similar topics |
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;
|
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...
|
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.
|
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...
|
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'?
| |
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;
|
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
=============
|
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 {
|
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...
|
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++.
|
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,...
| |
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...
|
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,...
|
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...
|
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...
|
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();...
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |