- 论坛徽章:
- 0
|
请教关于文件读写问题.
这个用sed很方便
- sed -i '/李德/s/1939/1940/' filename
复制代码
简易c++代码:
- #include <iostream>;
- #include <fstream>;
- #include <string>;
- using namespace std;
- void printUsage)
- {
- cout << "Usage: ./a.out <fileName>; <keyWord>;
- <oldWord>; <newWord>;"
- <<endl;
- }
- int
- main(int argc,char **argv)
- {
- if(argc != 5)
- printUsage(),exit(1);
- fstream in(argv[1],ios::in | ios::out);
- string record;
- while(getline(in,record)) {
- string key(argv[2]),old(argv[3]),new(argv[4]);
- if (record.find(key,0) != string::npos) {
- size_type pos = record.find(old, 0);
- record.replace(pos, old.size(), new, 0, new.size());
- in.seekp(in.tellg() - record.size() - 1);
- in <<record << endl;
- break;
- }
- }
- }
复制代码 |
|