Help with file management. Please help!!

Discussion in 'C++' started by Tom, Nov 8, 2003.

  1. Tom

    Tom Guest

    OK I'm trying to go to the end of a file and delete the contents
    upwards until I meet a certain character. How can I do this? (NOTE:
    I'm using text files)
     
    Tom, Nov 8, 2003
    #1
    1. Advertising

  2. Tom

    Ray Gardener Guest

    If you don't mind brute force you can do:

    farthest_pos = 0
    open file
    while not eof
    read char
    if it is target char
    farthest_pos = current_pos
    wend
    rewind
    copy from 0 to farthest_pos to temp file
    close file
    remove file
    rename temp file to original file


    "Tom" <> wrote in message
    news:...
    > OK I'm trying to go to the end of a file and delete the contents
    > upwards until I meet a certain character. How can I do this? (NOTE:
    > I'm using text files)
     
    Ray Gardener, Nov 8, 2003
    #2
    1. Advertising

  3. Tom wrote:
    > OK I'm trying to go to the end of a file and delete the contents
    > upwards until I meet a certain character. How can I do this? (NOTE:
    > I'm using text files)


    This is very painful (at least to most operating systems). From
    reading articles in news:comp.lang.c and news:comp.lang.c++, there
    is no constant or standard method to position a file to a offset
    measured in units of characters. With this knowledge, backspacing
    through a file character by character is not portable. A case in
    point is "\r\n": is it one or two characters?

    Another rule about files that is hindering your activitiy is that
    files don't shrink. You really can't "delete" a character in the
    middle or end without creating a new file. The standard idiom
    for delete sections in a file is to copy all the "wanted" stuff
    to a new file and delete the old file.

    Given the above points, a likely process is to read the entire
    file into a container, then seach the container. A stack seems
    very appropriate. This algorithm could be modified by searching
    the file for the given character and saving the file positions.
    After the file is processed, rewind it and copy everything up
    to the last position saved to a new file. Two passes of reading
    the file are required; but less memory is consumed. This latter
    technique is portable since you are saving the position of a file
    which is generated by the OS and not modifying.

    --
    Thomas Matthews

    C++ newsgroup welcome message:
    http://www.slack.net/~shiva/welcome.txt
    C++ Faq: http://www.parashift.com/c -faq-lite
    C Faq: http://www.eskimo.com/~scs/c-faq/top.html
    alt.comp.lang.learn.c-c++ faq:
    http://www.raos.demon.uk/acllc-c /faq.html
    Other sites:
    http://www.josuttis.com -- C++ STL Library book
    http://www.sgi.com/tech/stl -- Standard Template Library
     
    Thomas Matthews, Nov 8, 2003
    #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. Floris van Haaster

    Project management / bug management

    Floris van Haaster, Sep 23, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    1,272
    Jon Paal
    Sep 23, 2005
  2. pouet
    Replies:
    2
    Views:
    814
    Will Hartung
    Jul 30, 2004
  3. Replies:
    4
    Views:
    567
    Chris Uppal
    May 5, 2005
  4. KK
    Replies:
    2
    Views:
    722
    Big Brian
    Oct 14, 2003
  5. MuZZy
    Replies:
    7
    Views:
    1,829
    Mike Hewson
    Jan 7, 2005
Loading...

Share This Page