How to delete a line or a character?

D

Dong Ge

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);
}
 
K

Karthik Kumar

Dong said:
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 !


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

Dong Ge

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

Chris Torek

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

Karthik Kumar

Dong said:
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.

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

Karthik Kumar

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




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

Dong Ge

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?
 
D

Dong Ge

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!
 
J

Jason Curl

Dong said:
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.
 

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

Forum statistics

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

Latest Threads

Top