ab*****@yahoo.c om.tw wrote:
dear all :
please help me to slove the problem
as below code , it can't run .
i want to read a file and display.
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <assert.h>
int main()
{
std::ifstream data_file("2252 135.dat",std::i os::binary);
char *data_ptr;
This is a pointer. It can (but doesn't at the moment) point
to some char array. But as said: At the moment it doesn't
point to anywhere specific.
while (data_file.eof( ))
{
data_file.read( data_ptr,2);
And here you tell read() to put the data into a specific
memory location. You do this by providing read() with the
memory address of where to put the data, by passing data_ptr.
Thus data_ptr better holds the memory address of some memory
where the data can be put. At the moment data_ptr doesn't point
to any usable memory. That's why you have problems.
std::cout << *data_ptr ;
{
data_file.close ();
}
You don't need a pointer variable at all.
All read() wants from you is that you pass the address of some
memory area, where it can write the data to:
char data[2];
while( data_file.eof() )
{
data_file.read( data, 2 );
}
Some additional hints.
Using eof() the way you do, is almost always a sure sign of
desaster. in C++ eof() is not ment to be used this way. eof()
is used to figure out, why a previous read has failed. The
paradigm used in C++ for reading files is:
while( reading_data_wo rked ) {
process_data
}
// now figure out, why the above loop terminated. If it was
// because of eof(), then the file was read completely. If it was
// not because of eof() then the read operation has failed because
// of some error
if( ! eof() )
process_error
That's the way file reading is done in C++. It has the nice side effect
that it handles all types of file read problems easily (no disk, disk error,
network terminated, transmission error ...). To support this paradigm, look
at the return values of all type of read functions. All of them have a return
value which can immediatly be used to distinguish between 'read-worked' and
'read-failed'.
while( data_file.read( data, 2 ) )
std::cout << *data;
if( !data_file.eof( ) )
std::cout << "Error during read\n";
What about the 'magical constant' 2 in the argument list to read? What does
it denote? Well, it denotes the size of the buffer (and thus maximum amount
of data to be read). Wouldn't it be better, to not have to specify that number
literally, but instead have the compiler to figure it out?
char data[2];
while( data_file.read( data, sizeof( data ) )
...
Now, if you are going to change the buffer size from 2 to eg. 3 characters, you
simply do
char data[3];
and the number passed to read will be adjusted by the compiler
Another hint: When dealing with binary files, it is better to use unsigned char
and not plain vanilla char. The reason is: char may or may not be signed or
unsigned. It depends entirely on the compiler what gets choosen. If you explicitely
specify unsigned char you break free of this uncertainity. Besides: this most
often is indeed what you want to do: working with raw bytes. unsigned char is
perfect for that.
--
Karl Heinz Buchegger
kb******@gascad .at