ifstream::read Question

B

brad

How would I determine the number of bytes that is.read actually read?

// allocate memory
char * buffer;

while (!is.eof())
{

buffer = new char [chunk];

//read data as a block
is.read(buffer, chunk);

//write the read data to stdout
std::cout << buffer;

delete [] buffer;
}
 
C

Christopher

How would I determine the number of bytes that is.read actually read?

// allocate memory
char * buffer;

while (!is.eof())
{

buffer = new char [chunk];

//read data as a block
is.read(buffer, chunk);

//write the read data to stdout
std::cout << buffer;

delete [] buffer;
}

1) Open a web browser and go to www.google.com
2) Search for C++ ifstream
3) Click on of the top 4 links
4) Navigate to member function read
5) Read the following:

"The function gcount() is used with input streams, and returns the
number of characters read by the last input operation."

Sorry for the sarcasm, I can't help it :)
 
B

brad

function gcount() is used with input streams, and returns the
number of characters read by the last input operation."

Sorry for the sarcasm, I can't help it :)

That's OK... I'm used to it. I overlooked gcount... thanks for taking
the time to respond.

Brad
 
J

Jerry Coffin

How would I determine the number of bytes that is.read actually read?

// allocate memory
char * buffer;

while (!is.eof())
{

buffer = new char [chunk];

//read data as a block
is.read(buffer, chunk);

//write the read data to stdout
std::cout << buffer;

delete [] buffer;
}

While your original question has been answered, I feel obliged to point
out that the rest of this code falls somewhat short of what you might
want.

First of all, almost loop like your:

while (!is.eof())

will require extra work to get correct results. The stream's eof() will
only return true if you'd reached the end of the file before the last
time you called read.

Second, you're wasting a lot of time and effort on allocating and
deleting your buffer. Right now, you're allocating and deleting the
buffer with each iteration of the loop -- without seeming to accomplish
anything useful by doing so.

Finally, read() does NOT append a nul character to the buffer following
the data it reads, so your use of operator<< (which expects a char* to
represent a nul-terminated string) can cause a problem as well. Instead
of using operator<<, you should really use the ostream's write().

do {
char buffer[chunk];
is.read(buffer, chunk);
std::cout.write(buffer, is.gcount());
} while (is.gcount() > 0);
 
S

sas

How would I determine the number of bytes that is.read actually read?
     // allocate memory
     char * buffer;
     while (!is.eof())
       {
       buffer = new char [chunk];
       //read data as a block
       is.read(buffer, chunk);
       //write the read data to stdout
       std::cout <<  buffer;
       delete [] buffer;
       }

1) Open a web browser and go towww.google.com
2) Search for C++ ifstream
3) Click on of the top 4 links
4) Navigate to member function read
5) Read the following:

"The function gcount() is used with input streams, and returns the
number of characters read by the last input operation."

Sorry for the sarcasm, I can't help it :)

Stumbled on this post by googling for the problem :)
Thanks for the helpful post.
 

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

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top