文本的查找和替换c语言流程图,c++文本查找替换

#include // function

unique();

#include // iostream:

cin , cout , cerr

#include // file

stream: ifstream , ofstream

#include // string

type

#include // vector

type

#include // function

Sleep(), #include in linux

platform

using namespace std; // use the standard lib namespace

int main(int argc, char * argv[])

{

string str; // store the find string

string tstr; // store the tmp string while match, it get one

line from vector every time

vector ivec; // store the

lineno when match successfully

vector svec; // store

the content of text file

char buff[BUFSIZ]; // to get the content of text file

int lineno = 0; // line number

int cnt = 0; // counter for match times

int beg = 0; // to flag the begin position while

matching

char replace_flag; // flag whether replace or not

string::iterator it_str; // iterator for matching

string::iterator it_tstr; // the same

ifstream in("TextFile.txt"); // open a text file.

if (!in) { // check whether open file successfully.

cerr << "Error opening

file!";

return -1;

}

while (in.getline(buff, BUFSIZ)) { // read from the text file,

and push into the vector svec.

svec.push_back(buff);

}

in.close(); // close the ifstream

cout << "please input words to

search!" << endl;

getline(cin, str); // get the whole line instead of get one

word(cin >> str).

cout << "The words you input

is:\n" << str

<< endl;

for (vector::iterator

sit = svec.begin(); sit != svec.end(); ++ sit) {

beg = 0;

tstr = *sit; // tstr copy one line from svec by

iterator.

it_str = str.begin(); // initialization of the

iterator

it_tstr = tstr.begin(); // the same

while (true)

{

if ((it_str != str.end() &&

it_tstr != tstr.end()) // can't use the *iterator while iterator

reach the end of vector.

&& (*it_str == *it_tstr)) //

judge whether both iterator equal, if equal continue after

self-add

{

it_str ++;

it_tstr ++;

}

else

{

if (it_str == str.end()) // if iterator reach the end of str,

it means match successfully

{

cnt ++; // then cnt self-add,

beg = beg + str.size(); // reset the begin position

ivec.push_back(lineno); // push lineno into vector, so while

the size of this vector is not 0, it match at least 1

time.

}

else // this situation is match fail,

{

beg ++; // reset the begin position by beg self-add

if (tstr.size() < str.size() + beg )// if the

rest of tstr is short than the size of str, end this circulation

(the size is unsigned int)

break;

}

it_tstr = tstr.begin() + beg; // reset the iterator

it_str = str.begin(); // the same

}

}

lineno ++; // lineno self-add

}

if (ivec.size() > 0) // match

successfully

{

cout << "The words \""

<< str

<< "\" match the TextFile.txt

"

<< ivec.size()

<< " times."

<< endl;

cout << "lineno : content"

<< endl;

// delete the repeat line before display.

vector::iterator it =

unique(ivec.begin(), ivec.end());

ivec.erase(it, ivec.end());

for (it = ivec.begin(); it != ivec.end(); ++ it) // show the

match line with format:  lineno : content

{

cout << *it

<< " : "

<< svec[*it]

<< endl;

}

cout << "Would you like to

replace them ?(N/y)" << endl;

cin >> replace_flag;

if (replace_flag == 'Y' || replace_flag == 'y')

{

string rstr; // store the replace string

cout << "please input the string

you want to replace to:" <<

endl;

cin.get(); // get the '\n'

getline(cin, rstr);

vector::iterator it =

ivec.begin();

while (it != ivec.end()) //

{

beg = 0;

tstr = svec[*it]; // tstr only copy one line from match

line.

it_str = str.begin();

it_tstr = tstr.begin();

while (true) {

if ((it_str != str.end() &&

it_tstr != tstr.end())

&& (*it_str == *it_tstr))

{

it_str ++;

it_tstr ++;

}

else {

if (it_str == str.end())

{

string tstr1(tstr, 0, beg); // store the front part of

original string

string tstr2(tstr, beg + str.size(), tstr.size() - beg -

str.size()); // store the rest part of original

tstr = tstr1 + rstr + tstr2;

beg = beg + rstr.size();

}

else

{

beg ++;

if (tstr.size() < str.size() + beg)

break;

}

it_tstr = tstr.begin() + beg;

it_str = str.begin();

}

}

svec[*it] = tstr;

++ it;

}

ofstream out("TextFile.txt", ofstream::trunc); // open the

text file to write

if (!out)

{

cerr << "open file

error!";

return -1;

}

for (vector::iterator

sit = svec.begin(); sit != svec.end(); ++ sit)

{

out << *sit

<< endl; // write into the file

cout << *sit

<< endl; // write to screen

}

out.close(); // close the file

}

else // don't replace the match string

cout << "thank you for using

this program !" << endl;

}

else

cout << "The words \""

<< str

<< "\" match error(not found)!"

<< endl;

cout << "Now this progress will

sleep for 60 seconds before quit" <<

endl;

Sleep(60000); // sleep for 60 seconds before quit

return 0;

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值