I have to delete the whole line in the file (first word matches), how can I ?

Discussion in 'C Programming' started by santa19992000@yahoo.com, Dec 2, 2005.

  1. Guest

    I have a file called xyz.txt, if the first word matches, I have to
    delete the whole line, how can I do that. sample is below

    file xyz.txt
    =======
    SP 0/23/2345/345
    CL 00/34/3456/54

    In the above file, if the first word "SP" matches, then I have to
    delete the whole line in that file, How to delete. Thanks.
    , Dec 2, 2005
    #1
    1. Advertising

  2. Re: I have to delete the whole line in the file (first wordmatches), how can I ?

    writes:
    > I have a file called xyz.txt, if the first word matches, I have to
    > delete the whole line, how can I do that. sample is below
    >
    > file xyz.txt
    > =======
    > SP 0/23/2345/345
    > CL 00/34/3456/54
    >
    > In the above file, if the first word "SP" matches, then I have to
    > delete the whole line in that file, How to delete. Thanks.


    You can't really delete a line from a file. What you can do is create
    a new file containing everything except the line that you don't want.
    If you like, you can then rename the new file, replacing the original
    one.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Dec 2, 2005
    #2
    1. Advertising

  3. Guest

    wrote:
    > I have a file called xyz.txt, if the first word matches, I have to
    > delete the whole line, how can I do that. sample is below
    >
    > file xyz.txt
    > =======
    > SP 0/23/2345/345
    > CL 00/34/3456/54
    >
    > In the above file, if the first word "SP" matches, then I have to
    > delete the whole line in that file, How to delete. Thanks.


    The usual way of doing this, in pseudocode:

    foreach line in FILE {
    // only print out lines that does not begin with SP:
    if ( ! beginsWith(line, "SP")) {
    fprintf( TEMP, "%s", line );
    }
    }
    // on most OS renaming replaces the old file:
    rename( "TEMP", "FILE" );
    , Dec 2, 2005
    #3
  4. Keith Thompson <> wrote:

    > You can't really delete a line from a file. What you can do is create
    > a new file containing everything except the line that you don't want.


    Or come up with something foolish (as I did) such as the following...

    #include <stdio.h>
    #include <assert.h>
    #include <string.h>

    int main( void )
    {
    long int pos=0;
    FILE *fp=fopen( "test.txt", "r+" );
    char readbuf[256], storebuf[8<<10];
    int found=0, bytecount=0;
    assert( fp );
    while( fgets(readbuf,sizeof readbuf,fp) ) {
    if( strstr(readbuf,"test") ) {
    bytecount+=strlen( readbuf );
    found=1;
    }
    else if( !found ) {
    pos=ftell( fp );
    }
    else {
    strcat( storebuf, readbuf ); /* Assume storebuf is large enough */
    }
    }
    if( pos ) {
    fseek( fp, pos, SEEK_SET );
    fputs( storebuf, fp );
    }
    while( bytecount-- > 0 ) {
    fputc( 0, fp );
    }
    if( found ) {
    fputc( "\n" );
    }
    fclose( fp );
    return 0;
    }

    My thought was to alter the file to make it superficially appear that
    the lines had been deleted. Is the last "line" of the file (a string
    null characters followed by a newline) guaranteed to look like an
    empty string to the next program that reads it with fgets()? I of
    course don't present this as a good idea; I just thought it would be
    fun to try.

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
    Christopher Benson-Manica, Dec 3, 2005
    #4
  5. Christopher Benson-Manica said:

    > Keith Thompson <> wrote:
    >
    >> You can't really delete a line from a file. What you can do is create
    >> a new file containing everything except the line that you don't want.

    >
    > Or come up with something foolish (as I did) such as the following...
    >
    > #include <stdio.h>
    > #include <assert.h>
    > #include <string.h>
    >
    > int main( void )
    > {
    > long int pos=0;
    > FILE *fp=fopen( "test.txt", "r+" );
    > char readbuf[256], storebuf[8<<10];
    > int found=0, bytecount=0;
    > assert( fp );


    In C90, the behaviour of this assertion is undefined.

    assert(fp != NULL);

    is fine from a behaviour perspective. It's not fine, though, as an example
    of good programming practice. A quick hack, presumably?

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
    Richard Heathfield, Dec 3, 2005
    #5
  6. Richard Heathfield <> wrote:

    > In C90, the behaviour of this assertion is undefined.


    Does that mean I get to use the C99 escape hatch? :)

    > is fine from a behaviour perspective. It's not fine, though, as an example
    > of good programming practice. A quick hack, presumably?


    The whole program was really not a great idea from a programming
    practice perspective, so yes, a quick hack indeed.

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
    Christopher Benson-Manica, Dec 3, 2005
    #6
  7. Christopher Benson-Manica said:

    > Richard Heathfield <> wrote:
    >
    >> In C90, the behaviour of this assertion is undefined.

    >
    > Does that mean I get to use the C99 escape hatch? :)


    Only if you implement one! :)

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
    Richard Heathfield, Dec 3, 2005
    #7
    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. \A_Michigan_User\
    Replies:
    2
    Views:
    867
    \A_Michigan_User\
    Aug 21, 2006
  2. Frost
    Replies:
    8
    Views:
    495
    Vladimir S. Oka
    Feb 10, 2006
  3. J. J. Ramsey
    Replies:
    3
    Views:
    726
  4. Richard Schneeman
    Replies:
    16
    Views:
    448
    Daniel Bush
    Aug 27, 2008
  5. Steve
    Replies:
    6
    Views:
    77
Loading...

Share This Page