PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > file position
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
file position

Réponse
 
LinkBack Outils de la discussion
Vieux 30/06/2008, 08h02   #1
nkomli@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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.
  Réponse avec citation
Vieux 30/06/2008, 08h26   #2
Alf P. Steinbach
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: file position

* nkomli@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.

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


> With seekg it seems you can only
> move positions without knowing whether you've moved to a newline or
> not.


Yes.


Cheers, & hth.,

- Alf

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
  Réponse avec citation
Vieux 30/06/2008, 10h15   #3
James Kanze
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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
  Réponse avec citation
Vieux 30/06/2008, 11h49   #4
Alf P. Steinbach
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: file position

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


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


Cheers, & hth.,

- Alf

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
  Réponse avec citation
Vieux 30/06/2008, 14h35   #5
James Kanze
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 06h01.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,17113 seconds with 13 queries