From: Earl Purple on

kanze wrote:
>
> There is no way to access the string's already-allocated buffer
> from outside the string, so this would require getline to be a
> friend of std::string. I rather doubt that it is in most
> implementations. (A quick grep on the preprocessor output of a
> program which included <string> showed no friend in the g++
> implementation.)

It wouldn't need to write to it directly, it would call one of the
string methods insert() or append() that takes a const char * and a
length - you'd want to pass the length so string won't need to look for
the null character.

> > Besides that, I would assume that both methods would write to
> > a buffer before writing directly to the string / char-array.
>
> I presume you are talking about the buffering done in filebuf.

For ostream. And for FILE * it also uses a buffer.

> > I would hope that it reads ahead to find the terminating '\n'
> > character before checking the allocation length of the
> > std::string.
>
> I'm not sure how you propose to implement this. getline doesn't
> have access to the internals of filebuf, any more than it does
> to the internals of std::string. And the buffer in filebuf has
> a fixed length anyway, so there's no guarantee that you'd find
> the '\n' in it.

No and I don't know the methods of streambuf etc but I do know
char_traits<char> has a find method which returns a null pointer if the
character is not found. If there is no way for ostream to do this there
is something lacking, if not in the standard then in this particular
implementation (as the implementer of fstream can surely use any
specific additions underneath that they have added to its streambuf).


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: kanze on
Earl Purple wrote:
> kanze wrote:

> > There is no way to access the string's already-allocated
> > buffer from outside the string, so this would require
> > getline to be a friend of std::string. I rather doubt that
> > it is in most implementations. (A quick grep on the
> > preprocessor output of a program which included <string>
> > showed no friend in the g++ implementation.)

> It wouldn't need to write to it directly, it would call one of
> the string methods insert() or append() that takes a const
> char * and a length - you'd want to pass the length so string
> won't need to look for the null character.

Yes. I think I was thinking more in terms of stringbuf.

Of course, accessing the already allocated buffer only works if
there is one; the user must have called string::reserve before.

> > > Besides that, I would assume that both methods would write
> > > to a buffer before writing directly to the string /
> > > char-array.

> > I presume you are talking about the buffering done in filebuf.

> For ostream. And for FILE * it also uses a buffer.

> > > I would hope that it reads ahead to find the terminating '\n'
> > > character before checking the allocation length of the
> > > std::string.

> > I'm not sure how you propose to implement this. getline
> > doesn't have access to the internals of filebuf, any more
> > than it does to the internals of std::string. And the
> > buffer in filebuf has a fixed length anyway, so there's no
> > guarantee that you'd find the '\n' in it.

> No and I don't know the methods of streambuf etc but I do know
> char_traits<char> has a find method which returns a null
> pointer if the character is not found. If there is no way for
> ostream to do this there is something lacking, if not in the
> standard then in this particular implementation (as the
> implementer of fstream can surely use any specific additions
> underneath that they have added to its streambuf).

There's no way for an external class to access the streambuf
buffer. A derived class can do so, but all of the functions
which do so directly are protected, so the istream and getline
can't.

--
James Kanze GABI Software
Conseils en informatique orient?e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S?mard, 78210 St.-Cyr-l'?cole, France, +33 (0)1 30 23 00 34


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Earl Purple on

kanze wrote:
> There's no way for an external class to access the streambuf
> buffer. A derived class can do so, but all of the functions
> which do so directly are protected, so the istream and getline
> can't.

It is true that there is no standard "find" method in streambuf, but as
it is the same entity that implements streambuf as the one who
implements getline, i.e. the writer of this STL, they are free to put
in hidden extras for their own use. Thus they can add in some kind of
find method and allow getline to access it so that it doesn't have to
read from the streambuf one character at a time until the terminator is
found. Instead it could call the internal find method and then do one
big copy, assuming that to be more efficient.


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Alex Vinokur on

"crhras" <crhras(a)sbcglobal.net> wrote in message
news:TkoOg.1329$e66.993(a)newssvr13.news.prodigy.com...
>
> Wow ! I just used two different file IO methods and the performance
> difference was huge. Is there something that I am doing wrong? or is
> fgets() just that much faster than getline()?
>
> Here's the code I used :
>
> // SLOOOOOOW
> // -----------------
> std::string line;
> std::ifstream in(filename.c_str());
>
> while (std::getline(in, line,'\n'))
> {
> }
>
> // FAAAAAASSSSSTTTTT
> // ---------------------------
> FILE * fp;
> fp = fopen(filename.c_str(), "r");
>
> while (fgets(line, 512, fp) != NULL)
> {
> }

There are a lot of methods of copying files.

Various test cases can be seen at:
http://groups.google.com/group/sources/browse_frm/thread/64e45316dde35021

Results of comparative performance measurement can be seen here:
http://groups.google.com/group/log-files/browse_frm/thread/508ee2a154042a0c


--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]