tags:

views:

92

answers:

2

How could I search for a word in a certain file, and then replace that word with a different string of text. Like for example: In this paragraph find the word iPad and replace it with iPhone. I would like a code sample in C (or C++ if it is not possible without a third party library).

+2  A: 

Just use fscanf and the string functions, everything you need is in stdio.h and string.h, both part of the standard C library. I'm sorry, I'm not going to just give you a code sample, but check out cplusplus.com for information on things like strcmp, fscanf, and toupper (or tolower, depending, you can use these for case insensitivity)

Alex Hart
+4  A: 

Pseudocode:

while (not end of inputFile) {
    line = inputFile.readline()
    line = line.replace("iPad", "iPhone")
    tempFile.writeline(line)
}
inputFile.close()
tempFile.close()
delete inputFile
rename tempFile to inputFile
Andrew Cooper