开发者

how to used c++ variable inside system ( ) function

I have a .txt file containing 2 columns . I have to search for a pattern in the first column and delete that line from the file that matches .

I have to do this inside a c++ program.

What are the options I could use ?

I thou开发者_运维百科gh of using a system() function and call sed (which seems efficient). But I'm not sure how to use sed inside system (). The search pattern is a c++ variable and I'm not sure how to insert it inside sed

int pattern;
.
.
.

system ("sed -i '/pattern/d' file.txt");

I know it could take this form, but I appreciate any help from you, about how to use c++ variable as a search pattern inside sed


assemble in a std::string and then use c_str(). You might find ostringstream (from sstream header) useful for assembling the string.


std::string s = "sed -i '/" + pattern + "/d' file.txt";

system(s.c_str());
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜