Removing characters from a file

Discussion in 'C++' started by Tom, Sep 30, 2003.

  1. Tom

    Tom Guest

    Hi all,

    I've searched high and low (google, the FAQ & so on) and I can't seem to
    find any way of doing this. What I am trying to do is to edit text files
    (eg. ini files) and remove/replace data in the file without having to create
    new files and transferring the data and so on. So one function I was trying
    to build was a "removechars" function which would simply remove x chars from
    the file from the current position. The code may not be done as elegant as
    possible but here is what I have so far:-

    int removechars(fstream iofile,long lNum)
    {
    long lLen = filelength(iofile), lCurr;
    char *sTemp;

    lCurr = iofile.tellg();

    if (lLen + 1 - lCurr - lNum > 0)
    sTemp = new char[lLen + 1 - lCurr - lNum];
    else
    return false;

    iofile.seekg(lNum,ios::cur);
    savetostr(sTemp,iofile);

    iofile.seekg(lCurr,ios::beg);
    iofile.write(sTemp,lLen - lCurr - lNum);

    // Here is the problem. I can't end the file at this current position

    iofile.seekg(lCurr,ios::beg);
    delete [] sTemp;
    return true;
    }

    So as noted in the code snippet, I can't work out how to alter the
    terminating position of the file. I shuffle everything forward the set
    number of characters but can't seem to be able to work out the finishing
    touch, and subsequently, there are trailing characters. I've tried throwing
    null in at the end but in a text file that just comes up as a space in my
    text editor and the rest of the characters still appear behind. I'm guessing
    there is some sort of way to set the eof flag at the new position I want but
    so far no luck going that way.

    Any help is greatly appreciated!
    Tom
     
    Tom, Sep 30, 2003
    #1
    1. Advertising

  2. Tom

    Tom Guest

    Forgot to mention what some of the other functions I was using do.

    long lLen = filelength(iofile) // self explanatory. Debugger shows this is
    working as expected so I didn't write what that does.

    savetostr(sTemp,iofile); // this function just copies the characters from
    the current position in the string onwards into the temporary string. It
    returns the stream pointer back to where it originally was

    Thanks,
    Tom
     
    Tom, Sep 30, 2003
    #2
    1. Advertising

  3. Tom

    Jack Klein Guest

    On Tue, 30 Sep 2003 13:27:14 +1000, "Tom" <> wrote in
    comp.lang.c++:

    > Hi all,
    >
    > I've searched high and low (google, the FAQ & so on) and I can't seem to
    > find any way of doing this. What I am trying to do is to edit text files
    > (eg. ini files) and remove/replace data in the file without having to create
    > new files and transferring the data and so on. So one function I was trying
    > to build was a "removechars" function which would simply remove x chars from
    > the file from the current position. The code may not be done as elegant as
    > possible but here is what I have so far:-


    [snip]

    There is literally no guaranteed way to do this. It is not a
    requirement of the language that it be able to produce this result,
    and there are operating systems that do not permit it.

    There are some particular system calls on some particular compiler/OS
    combinations that might allow this to work, but it is non-standard and
    non-portable.

    Just rewrite the file and you code will be portable everywhere.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c /faq
     
    Jack Klein, Sep 30, 2003
    #3
  4. Tom

    Tom Guest

    > There is literally no guaranteed way to do this. It is not a
    > requirement of the language that it be able to produce this result,
    > and there are operating systems that do not permit it.


    Seems like a crazy thing to omit from an OS to me... ah well guess I'll have
    to make do.

    > There are some particular system calls on some particular compiler/OS
    > combinations that might allow this to work, but it is non-standard and
    > non-portable.
    >
    > Just rewrite the file and you code will be portable everywhere.



    Thanks for the response,
    Tom
     
    Tom, Sep 30, 2003
    #4
  5. Tom

    Mike Wahler Guest

    "Tom" <> wrote in message news:3f793998$...
    > > There is literally no guaranteed way to do this. It is not a
    > > requirement of the language that it be able to produce this result,
    > > and there are operating systems that do not permit it.

    >
    > Seems like a crazy thing to omit from an OS to me...


    All operating systems do not serve the same purpose.
    Making such generalizations about all of them doesn't
    make sense. E.g. some computer systems don't have
    'files' at all. I've worked with a few.

    >ah well guess I'll have
    > to make do.


    If your operating system has a file system, it will have
    an interface (API) for your program to use to do whatever
    it allows. But this is outside the domain of the C++
    language. Check your OS documentation.

    >
    > > There are some particular system calls on some particular compiler/OS
    > > combinations that might allow this to work, but it is non-standard and
    > > non-portable.
    > >
    > > Just rewrite the file and you code will be portable everywhere.


    Also, in your original post, you talked about "e.g. ini files".
    I'm guessing these are 'configuration information' as in Windows
    ..ini files. Generally these files are relatively small, and
    rewriting them completely will not take noticably longer than
    trying to edit them 'in place'. I suspect you're anticipating
    a performance problem that doesn't exist.

    Only optimize after you've *proven*, via profiling, or customer
    feedback, that performance really is a problem.

    -Mike
     
    Mike Wahler, Sep 30, 2003
    #5
  6. Tom

    Tom Guest

    > > > There are some particular system calls on some particular compiler/OS
    > > > combinations that might allow this to work, but it is non-standard and
    > > > non-portable.
    > > >
    > > > Just rewrite the file and you code will be portable everywhere.

    >
    > Also, in your original post, you talked about "e.g. ini files".
    > I'm guessing these are 'configuration information' as in Windows
    > .ini files. Generally these files are relatively small, and
    > rewriting them completely will not take noticably longer than
    > trying to edit them 'in place'. I suspect you're anticipating
    > a performance problem that doesn't exist.


    Yes I just have a preconceived idea that it was an issue and I guess it just
    seemed more logical to me that I'd be able to edit files. I don't want to do
    too much OS specific programming, particularly for something not interface
    related, so I think I'll stick to the 'create new file' idea rather than
    moving into system calls.

    Thanks again,
    Tom
     
    Tom, Oct 1, 2003
    #6
    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. Barry Gilmore

    Removing return characters from a string

    Barry Gilmore, Oct 17, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    1,117
    Peter Rilling
    Oct 17, 2005
  2. SatishPasala

    Removing wild characters from a string

    SatishPasala, Nov 29, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    2,706
    SatishPasala
    Nov 29, 2005
  3. Replies:
    5
    Views:
    282
    Stefan Neumann
    Feb 9, 2006
  4. Chris  Chiasson
    Replies:
    6
    Views:
    638
    Richard Tobin
    Nov 14, 2006
  5. rvino
    Replies:
    0
    Views:
    4,694
    rvino
    Aug 14, 2007
Loading...

Share This Page