473,396 Members | 1,866 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

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 '05 #1
6 2340
sa***********@yahoo.com 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) ks***@mib.org <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.
Dec 2 '05 #2
sa***********@yahoo.com 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 '05 #3
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.
Dec 3 '05 #4
Christopher Benson-Manica said:
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 );


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)
Dec 3 '05 #5
Richard Heathfield <in*****@invalid.invalid> 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.
Dec 3 '05 #6
Christopher Benson-Manica said:
Richard Heathfield <in*****@invalid.invalid> 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)
Dec 3 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: | last post by:
Hi all, I've been using C++ for quite a while now and I've come to the point where I need to overload new and delete inorder to track memory and probably some profiling stuff too. I know that...
7
by: nephish | last post by:
Hey there, i have a text file with a bunch of values scattered throughout it. i am needing to pull out a value that is in parenthesis right after a certain word, like the first time the word...
12
by: deko | last post by:
Is there a quick and dirty way to delete all files in a given directory? perhaps something like this: Set objFile = CreateObject("Scripting.FileSystemObject") objFile.DeleteFile "all files in...
5
by: ad | last post by:
I want to delete some text which begin with <!DOCTYPE and end with > in a file. How can I do it?
4
by: shonend | last post by:
I am trying to extract the pattern like this : "SUB: some text LOT: one-word" Described, "SUB" and "LOT" are key words; I want those words, everything in between and one word following the...
5
by: MrNobody | last post by:
is there a simple way to make it so your regex only matches whole words? i was thinking simply something like: *match_string* but then I think it would fail if the word was at the beginning...
1
by: Michael Yanowitz | last post by:
Hello: I am hoping someone knows if there is an easier way to do this or someone already implemented something that does this, rather than reinventing the wheel: I have been using the...
19
by: =?Utf-8?B?QnJpYW4gQ29vaw==?= | last post by:
This is an example of the data; 2007/07/27 11:00:03 ARES_INDICATION 010.050.016.002 404.2.01 (6511) RX 74 bytes 2007/07/27 11:00:03 65 11 26 02 BC 6C AA 20 76 93 51 53 50 76 13 48...
2
by: Francesco Pietra | last post by:
Please, how to adapt the following script (to delete blank lines) to delete lines containing a specific word, or words? f=open("output.pdb", "r") for line in f: line=line.rstrip() if line:...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.