468,247 Members | 1,262 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,247 developers. It's quick & easy.

ifstream::read Question

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;
}
Jun 27 '08 #1
5 2935
On May 28, 9:05 am, brad <byte8b...@gmail.comwrote:
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 :)
Jun 27 '08 #2
brad wrote:
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;
}
http://www.cplusplus.com/reference/i...ream/read.html
Jun 27 '08 #3
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
Jun 27 '08 #4
In article <g1**********@solaris.cc.vt.edu>, by*******@gmail.com says...
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);

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jun 27 '08 #5
sas
On May 28, 6:08*pm, Christopher <cp...@austin.rr.comwrote:
On May 28, 9:05 am, brad <byte8b...@gmail.comwrote:
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.
Jun 27 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Gunnar | last post: by
5 posts views Thread by Nils Wogatzky | last post: by
11 posts views Thread by flips | last post: by
2 posts views Thread by Kitty | last post: by
12 posts views Thread by Steven T. Hatton | last post: by
1 post views Thread by Alex Vinokur | last post: by
4 posts views Thread by supemoy | last post: by
1 post views Thread by Sergei Shelukhin | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by kermitthefrogpy | last post: by
reply views Thread by zattat | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.