0

How do I replace a word in a file with another word using C?

For example, I have a file which contains:

my friend name is sajid 

I want to replace the word friend with grandfather, such that the file is changed to:

my grandfather name is sajid 

(I am developing on Ubuntu Linux.)

Update:

I am doing filing in C. I have created a .txt file and write some data into it, but as my program progresses I have to search some text and replace it with the other words. The problem I am facing is that suppose in my file I wrote

"I bought apple from the market" 

If i replace apple with pineapples as apple has 5 char and pineapple has 9 char it will write it as

"I bought pineapple m the market" 

It also has affected the words written after apple.

I have to do it using C, not a command line script.

5
  • 2
    please show the code you're using that's not working. Commented Feb 18, 2011 at 14:04
  • You have to learn how to ask questions first. I bet that if you rephrase, and clearly state your problem, you will find a solution yourself. See catb.org/~esr/faqs/smart-questions.html Commented Feb 18, 2011 at 14:12
  • Im a bit confused at what your asking? Or what language/program your using? Since most text editors have a Replace Function? Commented Feb 18, 2011 at 14:15
  • 1
    Similar questions : stackoverflow.com/questions/2672581/… and stackoverflow.com/questions/1733058/… and stackoverflow.com/questions/2211749/… Commented Feb 18, 2011 at 14:30
  • Hi mainajaved, have you solved the problem? I have the same problem as you. It will be nice if you could share the solution here. Commented Jun 18, 2013 at 8:01

2 Answers 2

1

How about using the exiting Linux sed program?

sed -i 's/friend/grandfather/' filename

That will replace friend with grandfather in the existing file. Make a copy first if you want to keep the original!

Edit:

Alternatively, load the file into an STL string, replace 'friend' with 'grandfather' using a technique such as this, and save the new string into a file.

Sign up to request clarification or add additional context in comments.

5 Comments

He did mention that he wanted to do it in C.
I know, but using sed would save him having to code anything. It isn't clear if sajid has to write the code, or is writing the code just to do search/replace on the file.
It sounds like a task which one would do to learn C (or perhaps homework?). Using sed wouldn't be of much benefit if that's the case.
True. Would spawning sed from C be classed as cheating? ;)
I believe it would be! Nice try... :)
1

As you've realized, you won't be able to make the change in place in the original file.

The easy solution is to read each string from the original file and write it to standard output, making the replacement as necessary. In pseudocode:

open original file while not at end of original file get next string from original file if string == "friend" write "grandfather" to standard output else write string to standard output end while 

You can then redirect the output to another file when you execute it, similar to how sed and awk work.

Alternately, you can create a destination file in the code and write to it directly. If you need to replace the original file with the new file, you can use the remove() and rename() library functions in stdio.

1 Comment

thanks so much John Bode got your point but also i have one other problem that continously updating and every line of file has differnt word to replace with i you want i will explain you my program

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.