[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[lmi] Reading an empty file into a string
From: |
Greg Chicares |
Subject: |
[lmi] Reading an empty file into a string |
Date: |
Sun, 21 Sep 2008 14:27:22 +0000 |
User-agent: |
Thunderbird 2.0.0.16 (Windows/20080708) |
We've encountered an 'istream_to_string.hpp' problem with msvc,
in this code:
if(is.rdbuf()->in_avail())
{
oss << is.rdbuf();
}
because msvc's in_avail() always returns zero, as is apparently
permitted:
http://groups.google.com/group/comp.lang.c++.moderated/msg/f3788f581b712d1e
It still seems desirable for this function template to produce an
empty string from an empty file (more precisely, from an fstream
corresponding to an empty file), without throwing an exception.
Is there a tidy and reliable way to do that?
I added a unit test on 20080921T1309Z to make this easier to
investigate, and also to compare performance of two methods:
Method 0: construct string from istreambuf_iterator pair
Method 1: extract from rdbuf into stringstream; use its str()
of which the latter is used in production. Sample timings for a
30627-byte file:
MinGW gcc-3.4.4:
Method 0: 5.776e-004 s = 577611 ns, mean of 100 iterations
Method 1: 3.307e-004 s = 330730 ns, mean of 100 iterations
como-4.3.3:
Method 0: 2.030e-03 s = 2029539 ns, mean of 100 iterations
Method 1: 3.672e-03 s = 3671666 ns, mean of 100 iterations
- [lmi] Reading an empty file into a string,
Greg Chicares <=