473,503 Members | 2,163 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3555
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
8970
by: Gunnar | last post by:
Hello, I've just written a CPP program that reads integers from a binary file, and used this code while (my_ifstram.read( (char* ) &number, sizeof(int)) { // do something with number } My...
5
6303
by: Nils Wogatzky | last post by:
Hi, I´ve got a problem with the iftream.read method. I´m reading out a binary file, but I receive wrong values if values are negative. m_File.read((char*)&help2,2); so, help2 is...
11
2491
by: flips | last post by:
OK this is really simple but I can't think what is the best and most elegant solution. Say I am getting an int from an ifstream as follows: int val; inFile.get(reinterpret_cast<char*>(&val),...
4
2040
by: Clint Ruen | last post by:
Hello all, I have written out a data structure using the binary flag on an ofstream. The struct/class is something like this class SomeData { public: int data1;
2
292
by: Kitty | last post by:
I use " ifstream input_file("input.txt") " to open a file. I want to show a warning message if "input.txt" does not exist. How can I do for that? Thanks.
12
11631
by: Steven T. Hatton | last post by:
I know of a least one person who believes std::ifstream::read() and std::ofstream::write() are "mistakes". They seem to do the job I want done. What's wrong with them. This is the code I...
1
3304
by: Alex Vinokur | last post by:
Testsuites "Comparative Performance Measurement. Reading file into string" at http://groups.google.com/group/perfo/msg/8273f4d1a05cfbd1 http://groups.google.com/group/sources/msg/27a9b6f91239c909...
4
9058
by: supemoy | last post by:
I am having a heck of a time using the ifstream::read() method. I am using Visual Studio 6.0, and my intelisense indicates that read has an overload of: ifstream::read(unsigned char*, int _n) A...
1
2271
by: Sergei Shelukhin | last post by:
Hi. I have the following problem; VC++ project, there's this code operation on 500Mb file via ifstream, POS_TYPE for this case is unsigned int which is 32-bit long. //dataFile.good() returns 1...
0
7353
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7011
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...
0
7468
tracyyun
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...
0
5596
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4689
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...
0
3170
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1521
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 ...
1
747
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
401
bsmnconsultancy
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.