Search for a word in a file and replace it in C?
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 wi开发者_运维技巧thout a third party library).
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
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)
精彩评论