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

S

santa19992000

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.
 
K

Keith Thompson

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.
 
S

slebetman

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" );
 
C

Christopher Benson-Manica

Keith Thompson said:
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.
 
R

Richard Heathfield

Christopher Benson-Manica said:
Keith Thompson said:
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?
 
C

Christopher Benson-Manica

Richard Heathfield said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top