file position

Discussion in 'C++' started by nkomli@gmail.com, Jun 30, 2008.

  1. Guest

    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.
     
    , Jun 30, 2008
    #1
    1. Advertising

  2. James Kanze Guest

    On Jun 30, 9:26 am, "Alf P. Steinbach" <> wrote:
    > * :


    > > 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:
    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, Jun 30, 2008
    #2
    1. Advertising

  3. James Kanze Guest

    On Jun 30, 12:49 pm, "Alf P. Steinbach" <> wrote:
    > * James Kanze:


    > > On Jun 30, 9:26 am, "Alf P. Steinbach" <> wrote:
    > >> * :


    > >>> 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:
    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, Jun 30, 2008
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Luqman
    Replies:
    1
    Views:
    664
    Luqman
    Feb 7, 2006
  2. James Wong
    Replies:
    4
    Views:
    518
    James Wong
    Jul 14, 2004
  3. Replies:
    3
    Views:
    188
  4. James Black
    Replies:
    0
    Views:
    407
    James Black
    May 28, 2006
  5. brendan
    Replies:
    0
    Views:
    206
    brendan
    Aug 29, 2006
Loading...

Share This Page