On Jun 30, 9:26 am, "Alf P. Steinbach" <al...@start.nowrote:
* nko...@gmail.com:
When outputting or inputting to a file is there some easy
way to move to and overwrite a specific line?
No, not if you're talking about an ordinary text file.
If you know the length of the line, there is. You have to save
the position prior to reading the line.
That hasn't anything to do with C++, it has to do with the
structure of an ordinary text file.
Not really. The C++ standard says very clearly what you can and
cannot do with regards to seeking in a text or a binary file.
(Actually, I think I'm lying about "clearly". The C++ standard
defines pretty much everything concerning IO by reference to the
C standard, which means that you usually have to look in two
different standards. And even when it doesn't, "clear" and "the
C++ Standard" seems to be a oxymoron.)
With seekg it seems you can only move positions without
knowing whether you've moved to a newline or not.
Yes.
The one argument form of seekg just calls seekpos on the
streambuf. If the streambuf is in fact a filebuf, "If sp has
not been obtained by a previous successful call to one of the
positioning functions (seekoff or seekpos) on the same file the
effects are undefined." The two argument form calls
streambuf::seekoff, which for a filebuf is defined "as if" you
called fseek, which (in the C standard) says: "For a text
stream, either offset shall be zero, or offset shall be a value
returned by an earlier successful call to the ftell function on
a stream associated with the same file and whence shall be
SEEK_SET."
If you want to move around in an arbitrary fashion in a text
file, seeking according to the line number, you have to first
read the entire file (using getline, for example), saving the
results of ftell in an array.
--
James Kanze (GABI Software) email:ja*********@gmail.com
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