"Ulrich Eckhardt" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > [EMAIL PROTECTED] wrote: > > Here are two functions that read text and binary file into string. > > Alex, apart from being really horrible C++[1], do these functions do > anything different except that they operate on different streams for > input? I just scanned them visually and could not find any other > differences... > > Also, the code seems pretty convoluted, if anything there explicitly > matters, you completely fail to point out that difference.
Ulrich, those functions are part of 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 . There are 24 different C++-functions including two discussed ones. > > Uli > > [1]: I'm not sure if/where it matters, here's how it could have been > written differently: > > > string txt_file_to_string () > > If the two functions are the same, you could as well have passed the stream > as reference: > string to_string( std::ifstream& in) > or alternatively passing along the expected size. Of course. But to measure "pure" performance I prefered to avoid passing arguments. > > > ostringstream oss; > > Starting with this, there is an operator>> overload that takes a > streambuffer on the right hand side, so copying the file into that buffe > would be a mere > in >> oss.rdbuf(); Several of testsuites use rdbuf(). > > > vector<char> v (infile_txt_filesize); > > fs_infile_txt.read(&v[0], infile_txt_filesize); > > Could init the vector with the content of the file instead: > vector<char> v( (istream_iterator<char>(in)), istream_iterator()); > > > ostream_iterator<char> out(oss); > > copy (&v[0], &v[v.size()], out); > > You're thinking too complicated: > copy( v.begin(), v.end(), ostream_iterator<char>(oss)); I wanted to work here with pointers (not with iterators) in that functions. Other functions (in the testsuites) work with copy() and iterators. > > > return oss.str(); > > How about > return std::string( (istream_iterator<char>(in)), istream_iterator()); > here? Alternatively, something like this: > std::string res(infile_filesize); > std::copy( istream_iterator<char>(in), istream_iterator(), str.begin()); > [snip] Similar methods are used in other functions of the testsuites. Thanks, -- Alex Vinokur email: alex DOT vinokur AT gmail DOT com http://mathforum.org/library/view/10978.html http://sourceforge.net/users/alexvn _______________________________________________ Help-gplusplus mailing list [email protected] http://lists.gnu.org/mailman/listinfo/help-gplusplus
