how to modify a txt file

J

Jerry

Hi, everybody:

I want to modify a portion of a txt file. The way I do it is to
open the file in read mode, and create a new file, and delete the old
file, and rename the new file.
I am wondering if this is a common practice of modifying a file in
C, is there any other way to do it?
Thank you all for your attention.


--Jerry
 
J

Jacob Oost

Jerry said:
Hi, everybody:

I want to modify a portion of a txt file. The way I do it is to
open the file in read mode, and create a new file, and delete the old
file, and rename the new file.
I am wondering if this is a common practice of modifying a file in
C, is there any other way to do it?
Thank you all for your attention.


--Jerry

Couldn't you just open a new file for reading or writing?

fptr = fopen("filename", "r+");

Of course you'd do the standard error-checking. I'm new to this but
isn't that what you want? According to my book, "r+" opens an existing
file for reading or writing.

--

----- BEGIN GEEK CODE BLOCK -----
Version 3.1
GAT d? !s !a C++++ UL+ P L++ E- W+ N+ o-- K- w--
O- !M !V PS-- PE++ Y+ PGP- t++>++++* 5? !X-- R- tv b++ DI+ D++
G e !h !r !y
...... END GEEK CODE BLOCK ----
 
J

Jerry

Thanks, if it is just adding stuff at the end of an existing file, I'd
use your approach. But modifying the txt involves deleting things in a
file, and "inserting" things in the middle of a file. I am wondering if
there is a way to delete things in a file. Meanwhile, I dont think
"write" is as same as "insert", cause the file pointer does not care
what's already in there, and it will just erease things in the existing
file. therefore, if we want to use r+ mode, we need to take care of a
lot of things.
 
C

CBFalconer

Jerry said:
I want to modify a portion of a txt file. The way I do it is to
open the file in read mode, and create a new file, and delete the
old file, and rename the new file.
I am wondering if this is a common practice of modifying a file in
C, is there any other way to do it?

Yes, in any language. However you might think about how to recover
if any of those steps fail, and how to detect those failures.
 
J

Jerry

Thank you very much.
Yes, in any language. However you might think about how to recover
if any of those steps fail, and how to detect those failures.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
 

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,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top