Removing characters from a file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tom

    Removing characters from a file

    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 "removechar s" 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(fst ream iofile,long lNum)
    {
    long lLen = filelength(iofi le), lCurr;
    char *sTemp;

    lCurr = iofile.tellg();

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

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

    iofile.seekg(lC urr,ios::beg);
    iofile.write(sT emp,lLen - lCurr - lNum);

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

    iofile.seekg(lC urr,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

    #2
    Re: Removing characters from a file

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

    long lLen = filelength(iofi le) // 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


    Comment

    • Jack Klein

      #3
      Re: Removing characters from a file

      On Tue, 30 Sep 2003 13:27:14 +1000, "Tom" <a@a.com> wrote in
      comp.lang.c++:
      [color=blue]
      > 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 "removechar s" 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:-[/color]

      [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.l earn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq

      Comment

      • Tom

        #4
        Re: Removing characters from a file

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

        Seems like a crazy thing to omit from an OS to me... ah well guess I'll have
        to make do.
        [color=blue]
        > 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.[/color]


        Thanks for the response,
        Tom


        Comment

        • Mike Wahler

          #5
          Re: Removing characters from a file


          "Tom" <a@a.com> wrote in message news:3f793998$1 _1@news.iprimus .com.au...[color=blue][color=green]
          > > 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.[/color]
          >
          > Seems like a crazy thing to omit from an OS to me...[/color]

          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.
          [color=blue]
          >ah well guess I'll have
          > to make do.[/color]

          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.
          [color=blue]
          >[color=green]
          > > 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.[/color][/color]

          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


          Comment

          • Tom

            #6
            Re: Removing characters from a file

            > > > There are some particular system calls on some particular compiler/OS[color=blue][color=green][color=darkred]
            > > > 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.[/color][/color]
            >
            > 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.[/color]

            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


            Comment

            Working...