please help me view the code

A

abmd.tw

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("2252135.dat",std::ios::binary);
char *data_ptr;

while (data_file.eof())
{
data_file.read(data_ptr,2);
std::cout << *data_ptr ;
{
data_file.close();
}

#thanks
 
K

Karl Heinz Buchegger

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("2252135.dat",std::ios::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_worked ) {
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.
 
H

Howard

Karl Heinz Buchegger said:
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:

Not only that, but his logic is reversed! Even if eof() could test for
end-of-file *before* the read, the above says "while end-of-file, read from
the file", which makes no sense at all. :)

[By the way, I've looked through some of my old notes and books from college
(yes, I kept them :)), and it seems that the "while not end of file"
concept is prevalent in examples. I suppose that, when moving from
pseudo-code to real code, a lot of programmers simply assume that the eof()
function will substitute directly for the pseudo-code. Obviously, in
standard C++, it won't. But I bet that's why so many (including me) start
out writing "while (!myfile.eof())", and only change it after seeing posts
like these.]

-Howard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top