You are here:

C++/How to edit a certain position in a text file using C++

Advertisement


Question
Hi,

I was wondering how do you find a certain word or line that I want in a text file and then replacing it with another word or line.   

For example,   

I have a text file that contains:   

[Printer number 3]
Enable=0
[Scanner number 15]
Enable=1   

How do I change "Scanner number 15" to "Scanner number 10", or change "Enable=1" to "Enable=0"?   

I know the basics for opening, closing, reading, and writing text files. I just need to know how to write to specific locations in my text. I tried searching for answers, and member functions such as tellp(), tellg(), seekg(), and seekp() came up, but I'm not really sure if they are needed or how they work.   

Your help is much appreciated.   

Thanks

Answer
One method is read a word using get() function setting ''for delim from the file. Compare it with word. If it matches with the word then move the file pointer to the location of the word and write the word to be replaced. Please, let me know whether it answers your question

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


rakshitha

Expertise

Difficult questions related to C .

Experience

C ,vc ,opc ,windows CE, ActiveX

Education/Credentials
Masters in electrical engineering

©2016 About.com. All rights reserved.