开发者

Search for a word in a file and replace it in C?

开发者 https://www.devze.com 2023-01-17 02:48 出处:网络
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

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)

0

精彩评论

暂无评论...
验证码 换一张
取 消