why does cout.tellp always return -1?
Asked Answered
B

1

7

I want to provide a tab-like capability for C++ text output streams. The feature should allow me to say "note this position", then allow multiple insert operations, and finally allow me to say "add enough fill characters so as to end up N characters past the originally noted position".

The standard iostream system does not seem to maintain a column position but I had thought that I could fake it using tellp(). My assumption was that the difference between tellp() at two points in my output sequence would correspond to the number of intervening bytes.

Unfortunately, at least in my Gnu C++ environment, cout does not maintain the fiction of a stream position. Every cout.tellp() call returns -1. Why is that?

Brescia answered 23/6, 2012 at 2:50 Comment(3)
tellp is never a column position in any stream. It is only meaningful in seekable streams.Wanderlust
@R.MartinhoFernandes is correct. tellp is supposed to tell you what value to provide to seekp to get back to the current position. In this case, it's telling you that there is no such possibility. Entabbing has to be supported some other way.Thrifty
Thank you both. It was the relationship to seekp that I failed to grasp.Brescia
C
5

tellp returns a position in a stream so that you can seek to it. Console does not allow seeking. Besides, even you interpret position as "the number of bytes written to the stream since it was created", that number won't be of any use for cursor positioning - the screen wraps around, its width is generally unpredictable. You just won't know what column you're on, since the row length is variable.

If you want reasonable cursor positioning on the screen, check out the ANSI terminal specification and escape commands that come with it. They allow for cursor position discovery and placement.

http://ascii-table.com/ansi-escape-sequences.php

In general, the screen is not a stream. Neither is the keyboard, for that matter :)

Chee answered 23/6, 2012 at 2:58 Comment(4)
Thanks Seva. It was the relationship between seekp and tellp that I failed to grasp.Brescia
Thanks @Seva. I am not trying to do cursor manipulation. For alignmnet I simply wanted to emit left justified values with trailing punctuation into fix width columns (e.g. "1: " and "12: "). Because this entails more that a single insert operation the standard width() manipulator is insufficient. If you reread my original question you will see that I did not intend to interpret tellp() as a column position. It was the difference between two calls to tellp() that I expected would allow me to determine how many bytes had been inserted between them.Brescia
I was looking for an answer to this question: How to interpret the number tellp() returns? Answer: "number of bytes written to the stream since it was created", thanks Seva, +1. I verified by checking the actual size of the file non-programmatically.Tychon
Not necessarily. If you call seek after writing, the current position will not be at the end of the file, and tellp will respect that.Chee

© 2022 - 2024 — McMap. All rights reserved.