file position

N

nkomli

When outputting or inputting to a file is there some easy way to move
to and overwrite a specific line? With seekg it seems you can only
move positions without knowing whether you've moved to a newline or
not.
 
J

James Kanze

* (e-mail address removed):
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.)

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.
 
J

James Kanze

* James Kanze:
You're talking about a special case where (1) you've been at
that line sometime earlier, (2) you saved the position then,
and (3) the new line is exactly the same length as the old.
There are often such rare corener special cases that are
possible for something that isn't possible in general.
It's quite misleading to put that up as if it contradicted
what I wrote.

Yes and no. I forget what you'd actually written, but it's not
important. Such cases are rare, unless you actively take steps
to create them.
Really. ;-)
For the general problem, it's the same in any language.

Is it? Or maybe the question is: what is the general problem?
We regularly seek to arbitrary lines in files here, using the
Posix interface (which doesn't distinguish between text files
and binary files). Of course, you can't reliably do it under
Posix in an arbitrary file written with the editor; we also
write the files, and take care that all of the lines have the
same known length. I've done similar things in Fortran, in the
past. The general problem here is that the C++ language doesn't
provide an interface for this sort of things; other languages
do.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top