Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > How to delete a line or a character?

Reply
Thread Tools

How to delete a line or a character?

 
 
Dong Ge
Guest
Posts: n/a
 
      10-04-2004
Hi!
I am a beginner of C.

I want to delete some characters or a whole line in a text file. I
have tried the "fprintf", "fputs", "fwrite", but no one of them can
run rightly.

In the below codes, the "str1" is one text line , in fact. I want
replace "str1" (the whole line) with the "str3". In my case, "str3" is
shooter than "str1", But I will get some strange code that can not be
read.

Thanks in advance!

if ((cfPtr = fopen("client.dat","r+")) == NULL)
printf ("File counld not be opened. \n");
else
{
while (!feof(cfPtr))
{fgets(str1, 200, cfPtr);
printf ("OK!");
if (strstr(str1, str2))
printf ("NO!");
{fseek (cfPtr, ftell(cfPtr)-strlen(str1)-1,
SEEK_SET);
fwrite (str3, strlen(str1), 1, cfPtr);
exit();
}
}
fclose(cfPtr);
}

--
Dong Ge
2004-10-04
 
Reply With Quote
 
 
 
 
Karthik Kumar
Guest
Posts: n/a
 
      10-04-2004
Dong Ge wrote:
> Hi!
> I am a beginner of C.
>
> I want to delete some characters or a whole line in a text file. I
> have tried the "fprintf", "fputs", "fwrite", but no one of them can
> run rightly.
>
> In the below codes, the "str1" is one text line , in fact. I want
> replace "str1" (the whole line) with the "str3". In my case, "str3" is
> shooter than "str1", But I will get some strange code that can not be
> read.
>
> Thanks in advance!
>
> if ((cfPtr = fopen("client.dat","r+")) == NULL)


You use cfPts to open a file.

> printf ("File counld not be opened. \n");
> else
> {
> while (!feof(cfPtr))
> {fgets(str1, 200, cfPtr);
> printf ("OK!");
> if (strstr(str1, str2))
> printf ("NO!");
> {fseek (cfPtr, ftell(cfPtr)-strlen(str1)-1,
> SEEK_SET);
> fwrite (str3, strlen(str1), 1, cfPtr);


You are using the same handle to write again ?
Are you sure you wanna do this ?
May be, you want to read the contents of the file to a buffer
and then write again.

You have not mentioned how you are opening the file !


> exit();


After writing, you are exiting. Well - you are not closing the file.
That would imply that:

* There is no guarantee that the file contents are going to be written.


* <OT>
whatever the lock acquired by the underlying OS over the file
resource may not be necessary given back. But I think this depends on
the implementation, but nevertheless this is something you have to pay
heed to.
</OT>.

--
Karthik.
http://akktech.blogspot.com .
 
Reply With Quote
 
 
 
 
Dong Ge
Guest
Posts: n/a
 
      10-05-2004
Hi! Karthik,

I guess your maybe not reading my codes carefully. I use the same
handle "cfPtr" to open, write and close the same file, not "cfPts". In
fact, the code can be compiled and run.

I just want to know how to delete a text line or some characters.

Thanks for your reply.
--
Dong Ge
2004-10-05



On Mon, 04 Oct 2004 14:46:52 -0700, Karthik Kumar
<(E-Mail Removed)> wrote:

>Dong Ge wrote:
>> Hi!
>> I am a beginner of C.
>>
>> I want to delete some characters or a whole line in a text file. I
>> have tried the "fprintf", "fputs", "fwrite", but no one of them can
>> run rightly.
>>
>> In the below codes, the "str1" is one text line , in fact. I want
>> replace "str1" (the whole line) with the "str3". In my case, "str3" is
>> shooter than "str1", But I will get some strange code that can not be
>> read.
>>
>> Thanks in advance!
>>
>> if ((cfPtr = fopen("client.dat","r+")) == NULL)

>
> You use cfPts to open a file.
>
>> printf ("File counld not be opened. \n");
>> else
>> {
>> while (!feof(cfPtr))
>> {fgets(str1, 200, cfPtr);
>> printf ("OK!");
>> if (strstr(str1, str2))
>> printf ("NO!");
>> {fseek (cfPtr, ftell(cfPtr)-strlen(str1)-1,
>> SEEK_SET);
>> fwrite (str3, strlen(str1), 1, cfPtr);

>
> You are using the same handle to write again ?
>Are you sure you wanna do this ?
> May be, you want to read the contents of the file to a buffer
>and then write again.
>
> You have not mentioned how you are opening the file !
>
>
>> exit();

>
> After writing, you are exiting. Well - you are not closing the file.
>That would imply that:
>
>* There is no guarantee that the file contents are going to be written.
>
>
>* <OT>
> whatever the lock acquired by the underlying OS over the file
>resource may not be necessary given back. But I think this depends on
>the implementation, but nevertheless this is something you have to pay
>heed to.
> </OT>.


 
Reply With Quote
 
Chris Torek
Guest
Posts: n/a
 
      10-05-2004
In article <news:(E-Mail Removed) >
Dong Ge <(E-Mail Removed)> wrote:
> while (!feof(cfPtr))


Bug here, virtually guaranteed. If you ever see "while (!feof(...))"
in a C program, look for the bug. There will be one, at least 99%
of the time.

Enormous hint: see the comp.lang.c FAQ, question 12.2.

For the answer to the question in the "Subject:" line, see the
comp.lang.c FAQ as well.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (4039.22'N, 11150.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
 
Reply With Quote
 
Karthik Kumar
Guest
Posts: n/a
 
      10-05-2004
Dong Ge wrote:
> Hi! Karthik,
>
> I guess your maybe not reading my codes carefully. I use the same
> handle "cfPtr" to open, write and close the same file, not "cfPts". In
> fact, the code can be compiled and run.


Oops - Sorry. That was a typo. But my question remains the same -
how could you use the same handle to read and write .

>
> I just want to know how to delete a text line or some characters.



Read the contents of the file.
Close the file.

Apply the filter on the contents that you have read in step 1.
Open a file and write the contents.


>
> Thanks for your reply.
> --
> Dong Ge
> 2004-10-05
>


And please don't top-post.

--
Karthik.
http://akktech.blogspot.com .
------------ And now a word from our sponsor ------------------
For a quality usenet news server, try DNEWS, easy to install,
fast, efficient and reliable. For home servers or carrier class
installations with millions of users it will allow you to grow!
---- See http://netwinsite.com/sponsor/sponsor_dnews.htm ----
 
Reply With Quote
 
Karthik Kumar
Guest
Posts: n/a
 
      10-05-2004
Karthik Kumar wrote:
> Dong Ge wrote:
>
>> Hi! Karthik,
>>
>> I guess your maybe not reading my codes carefully. I use the same
>> handle "cfPtr" to open, write and close the same file, not "cfPts". In
>> fact, the code can be compiled and run.

>
>
> Oops - Sorry. That was a typo. But my question remains the same - how
> could you use the same handle to read and write .
>
>>
>> I just want to know how to delete a text line or some characters.

>
>
>
> Read the contents of the file.
> Close the file.
>
> Apply the filter on the contents that you have read in step 1.
> Open a file and write the contents.


To add further,

remove the original file.
rename the new file to the original file.

--
Karthik.
http://akktech.blogspot.com .
 
Reply With Quote
 
Dong Ge
Guest
Posts: n/a
 
      10-05-2004
In the comp.lang.c FAQ :

Question 19.14:
How can I insert or delete a line (or record) in the middle of a file?
--------------------------------------------------------------------------------

Short of rewriting the file, you probably can't. The usual solution is
simply to rewrite the file. (Instead of deleting records, you might
consider simply marking them as deleted, to avoid rewriting.) See also
questions 12.30 and 19.13.

Question 12.30
I'm trying to update a file in place, by using fopen mode "r+",
reading a certain string, and writing back a modified string, but it's
not working.
--------------------------------------------------------------------------------

Be sure to call fseek before you write, both to seek back to the
beginning of the string you're trying to overwrite, and because an
fseek or fflush is always required between reading and writing in the
read/write "+" modes. Also, remember that you can only overwrite
characters with the same number of replacement characters; see also
question 19.14.

" ALSO, REMEMBER THAT YOU CAN ONLY OVERWRITE CHARACTERS WITH THE SAME
NUMBER OF REPLACEMENT CHARACTERS."
This means I can't write my code with the standdard lib?

----
Dong Ge
2004-10-05



On Tue, 05 Oct 2004 12:57:36 +0800, Dong Ge <(E-Mail Removed)> wrote:

>Hi! Karthik,
>
>I guess your maybe not reading my codes carefully. I use the same
>handle "cfPtr" to open, write and close the same file, not "cfPts". In
>fact, the code can be compiled and run.
>
>I just want to know how to delete a text line or some characters.
>
>Thanks for your reply.


 
Reply With Quote
 
Dong Ge
Guest
Posts: n/a
 
      10-05-2004
Hi! Karthik,
The orginal text file contained thousands of lines, if I always remove
the original file and rename the new file. Maybe the program will run
very slowly.

Tons of thanks for your help!
--
Dong Ge
2004-10-05

On Mon, 04 Oct 2004 22:09:01 -0700, Karthik Kumar
<(E-Mail Removed)> wrote:

>Karthik Kumar wrote:
>> Dong Ge wrote:
>>
>>> Hi! Karthik,
>>>
>>> I guess your maybe not reading my codes carefully. I use the same
>>> handle "cfPtr" to open, write and close the same file, not "cfPts". In
>>> fact, the code can be compiled and run.

>>
>>
>> Oops - Sorry. That was a typo. But my question remains the same - how
>> could you use the same handle to read and write .
>>
>>>
>>> I just want to know how to delete a text line or some characters.

>>
>>
>>
>> Read the contents of the file.
>> Close the file.
>>
>> Apply the filter on the contents that you have read in step 1.
>> Open a file and write the contents.

>
> To add further,
>
> remove the original file.
> rename the new file to the original file.


 
Reply With Quote
 
Jason Curl
Guest
Posts: n/a
 
      10-05-2004
Dong Ge wrote:
> In the comp.lang.c FAQ :
>
> Question 19.14:
> How can I insert or delete a line (or record) in the middle of a file?
> --------------------------------------------------------------------------------
>
> Short of rewriting the file, you probably can't. The usual solution is
> simply to rewrite the file. (Instead of deleting records, you might
> consider simply marking them as deleted, to avoid rewriting.) See also
> questions 12.30 and 19.13.
>
> Question 12.30
> I'm trying to update a file in place, by using fopen mode "r+",
> reading a certain string, and writing back a modified string, but it's
> not working.
> --------------------------------------------------------------------------------
>
> Be sure to call fseek before you write, both to seek back to the
> beginning of the string you're trying to overwrite, and because an
> fseek or fflush is always required between reading and writing in the
> read/write "+" modes. Also, remember that you can only overwrite
> characters with the same number of replacement characters; see also
> question 19.14.
>
> " ALSO, REMEMBER THAT YOU CAN ONLY OVERWRITE CHARACTERS WITH THE SAME
> NUMBER OF REPLACEMENT CHARACTERS."
> This means I can't write my code with the standdard lib?

Of course you could try padding with spaces if that is compatible with
how you read the file again at a later time.
>
> ----
> Dong Ge
> 2004-10-05
>
>
>
> On Tue, 05 Oct 2004 12:57:36 +0800, Dong Ge <(E-Mail Removed)> wrote:
>
>
>>Hi! Karthik,
>>
>>I guess your maybe not reading my codes carefully. I use the same
>>handle "cfPtr" to open, write and close the same file, not "cfPts". In
>>fact, the code can be compiled and run.
>>
>>I just want to know how to delete a text line or some characters.
>>
>>Thanks for your reply.

>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Read a file line by line and write each line to a file based on the5th byte scad C++ 23 05-17-2009 06:11 PM
Open file, get first line, delete first line close file Richard Schneeman Ruby 16 08-26-2008 11:54 PM
How to read a text file line by line and remove some line kaushikshome C++ 4 09-10-2006 10:12 PM
Read a file line by line with a maximum number of characters per line Hugo Java 10 10-18-2004 11:42 AM



Advertisments