Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   file position (http://www.velocityreviews.com/forums/t622978-file-position.html)

nkomli@gmail.com 06-30-2008 07:02 AM

file position
 
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.

James Kanze 06-30-2008 09:15 AM

Re: file position
 
On Jun 30, 9:26 am, "Alf P. Steinbach" <al...@start.no> wrote:
> * 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:james.kanze@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

James Kanze 06-30-2008 01:35 PM

Re: file position
 
On Jun 30, 12:49 pm, "Alf P. Steinbach" <al...@start.no> wrote:
> * James Kanze:


> > On Jun 30, 9:26 am, "Alf P. Steinbach" <al...@start.no> wrote:
> >> * 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.


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

> >> That hasn't anything to do with C++, it has to do with the
> >> structure of an ordinary text file.


> > Not really.


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

--
James Kanze (GABI Software) email:james.kanze@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


All times are GMT. The time now is 08:54 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.