Keith Thompson <ks***@mib.org> 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.