Topic: Length of cout'ed string
Author: James Kuyper <kuyper@wizard.net>
Date: 1998/09/07 Raw View
Siemel Naran wrote:
>
> On 4 Sep 1998 22:16:44 GMT, James Kuyper <kuyper@wizard.net> wrote:
> >Siemel Naran wrote:
>
> >> Take a look at pcount() gcount(), tellp() tellg().
>
> >Don't pcount() and gcount() only give the length of the buffer, and not
> >the total number of characters written?
>
> First, there's no such thing as pcount().
It's in CD2, section 31.7.3 "Class ostream". Did they remove it from the
FDIS? If so, why?
> >Is there any way to compare two pos_type values, such as are returned by
> >tellp(), to determine the number of characters output between the
> >positions they represent?
>
> My guess: just subtract the two tellp values. If at point A, you
> calculate A=cout.tellp(), and at a later point B you calculate
> B=cout.tellp(), then (B-A) should be the number of chars written
> between these two points. But doing a cout.flush() messes things
> up on my implementation.
I found no wording in the standard that requires that to work. I get the
impression that pos_type could be a class, and that there are very few
member functions which that class is required to possess.
Even pos_type if it is an ordinary number, it might be an offset in the
file, rather than a count of the number of characters written to it.
Depending upon the operating system, the file offset might reflect many
things that are not bytes of C/C++ output.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: sbnaran@localhost.localdomain (Siemel Naran)
Date: 1998/09/08 Raw View
On 07 Sep 98 19:18:58 GMT, James Kuyper <kuyper@wizard.net> wrote:
>Siemel Naran wrote:
>> First, there's no such thing as pcount().
>It's in CD2, section 31.7.3 "Class ostream". Did they remove it from the
>FDIS? If so, why?
OK. It's not in the <ostream> for my compilers.
But it makes sense to have pcount() just for the parallel structure.
seekg(), seekp()
tellg(), tellp()
gcount(), pcount()
g_helloworld(), p_helloworld() // just kidding, there's no such thing!
>> My guess: just subtract the two tellp values. If at point A, you
>> calculate A=cout.tellp(), and at a later point B you calculate
>> B=cout.tellp(), then (B-A) should be the number of chars written
>> between these two points. But doing a cout.flush() messes things
>> up on my implementation.
>I found no wording in the standard that requires that to work. I get the
>impression that pos_type could be a class, and that there are very few
>member functions which that class is required to possess.
>Even pos_type if it is an ordinary number, it might be an offset in the
>file, rather than a count of the number of characters written to it.
>Depending upon the operating system, the file offset might reflect many
>things that are not bytes of C/C++ output.
There is pos_type and off_type. Off_type is for offsets. Pos_types
is for absolute positions.
Someone pointed out that tellp() returns the pos_type in the current
buffer, not the absolute position. But this is stupid, IMHO. Of
what use is a pos_type in the buffer? Maybe it's useful for users
who want to use the streambuf directly. So this sort of tellp()
function belongs as a member function in class streambuf. But
for users of iostreams, all they care about is the absolute position,
not details about the buffer. So tellg() should always tell
absolute positions.
On both g++ and KCC, I wrote a program that printed tens of thousands
of lines to cout, along with a tellg() on each line. The values
printed were always increasing, which tells me that tellg() keeps
track of the number of chars printed. The internal buffer probably
overflowed at some point, I'm not sure. But the values of tellg()
kept on increasing, indicating that tellg() does not return the
position in the current buffer, but rather the absolute position.
Stroustrup says that tellp() etc only make sense for streams where
positioning makes sense. So for cout, tellp() should be
meaningless. But it's pretty useful for implementors to regard
tellp() for cout as the absolute number of chars written.
I found that doing a cout.flush() messed things up. Then
every successive call to tellp() returns -1. Why is this?
--
----------------------------------
Siemel B. Naran (sbnaran@uiuc.edu)
----------------------------------
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: sbnaran@fermi.ceg.uiuc.edu (Siemel Naran)
Date: 1998/09/04 Raw View
On 04 Sep 98 16:08:08 GMT, Martin Fabian
>int lenght = printf("01%s%s", "23", "456"); // results in length == 7
>
>cout << "01" << "23" << "456"; // how to get the length here???
Take a look at pcount() gcount(), tellp() tellg().
--
----------------------------------
Siemel B. Naran (sbnaran@uiuc.edu)
----------------------------------
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: James Kuyper <kuyper@wizard.net>
Date: 1998/09/04 Raw View
Siemel Naran wrote:
>
> On 04 Sep 98 16:08:08 GMT, Martin Fabian
>
> >int lenght = printf("01%s%s", "23", "456"); // results in length == 7
> >
> >cout << "01" << "23" << "456"; // how to get the length here???
>
> Take a look at pcount() gcount(), tellp() tellg().
The I/O library is the part of the standard I understand least well, so
the following questions may represent great confusion:
Don't pcount() and gcount() only give the length of the buffer, and not
the total number of characters written?
Is there any way to compare two pos_type values, such as are returned by
tellp(), to determine the number of characters output between the
positions they represent? Is tellp() even meaningful when the output is
not a file?
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: sbnaran@localhost.localdomain (Siemel Naran)
Date: 1998/09/05 Raw View
On 4 Sep 1998 22:16:44 GMT, James Kuyper <kuyper@wizard.net> wrote:
>Siemel Naran wrote:
>> Take a look at pcount() gcount(), tellp() tellg().
>Don't pcount() and gcount() only give the length of the buffer, and not
>the total number of characters written?
First, there's no such thing as pcount().
Second, gcount() gives the number of chars read by the last unformatted
read command, such as istream::read(), istream::getline().
>Is there any way to compare two pos_type values, such as are returned by
>tellp(), to determine the number of characters output between the
>positions they represent?
My guess: just subtract the two tellp values. If at point A, you
calculate A=cout.tellp(), and at a later point B you calculate
B=cout.tellp(), then (B-A) should be the number of chars written
between these two points. But doing a cout.flush() messes things
up on my implementation.
> Is tellp() even meaningful when the output is
>not a file?
No. tellp() and tellg() are only meaningful for streams where positioning
makes sense. However, I have found that for cout, tellp() gives a
running count of the number of chars printed. For cerr, tellp()
gives -1. On cout, doing a flush() or seekp() causes every
successive call to tellp() to yield -1. What's going on here?
-----
#include <iostream.h>
#include <iomanip.h>
void action(ostream& strm)
{
int tellp=0;
strm << "Hello:";
tellp=strm.tellp();
strm << tellp << '\n';
strm << "World:";
tellp=strm.tellp();
strm << tellp << '\n';
strm.flush();
}
int main()
{
for (int i=0; i<3; i++) action(cout);
}
--
----------------------------------
Siemel B. Naran (sbnaran@uiuc.edu)
----------------------------------
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Martin Fabian <fabian@NOSPAM.control.chalmers.se>
Date: 1998/09/04 Raw View
--------------4A8D26E4BD330D190F7A2FA4
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
In the "good old days" when using printf it was easy to get the length
of the printed string, since printf returned that length. However, with
cout, how on earth to you get the length of the cout'ed string?
int lenght = printf("01%s%s", "23", "456"); // results in length == 7
cout << "01" << "23" << "456"; // how to get the length here???
--------------4A8D26E4BD330D190F7A2FA4
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: 7bit
<HTML>
In the "good old days" when using printf it was easy to get the length
of the printed string, since printf returned that length. However, with
cout, how on earth to you get the length of the cout'ed string?
<P><TT>int lenght = printf("01%s%s", "23", "456"); // results in length
== 7</TT><TT></TT>
<P><TT>cout << "01" << "23" << "456"; // how to get the
length here???</TT></HTML>
--------------4A8D26E4BD330D190F7A2FA4--
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]