I have to write a C/C++ program to process a bunch of text files (around 100) and find a pattern (commonly a string). Since the platform I am going to run this will be unix, I thought why wouldn't I make use of the grep system command within my program as it is very fast and effective. But, my friend says using 开发者_运维百科system("grep...") within a program is not advisable. He suggests me to use string pattern matching algorithm which I feel will slow down the program.
So, I want some advice over this. Help me out.
Without knowing what your program is going to do, it's hard to say. But running commands via system() will slow your program, down considerably, though this may not be important. Whatever you do, don't write your own string-matching code if regular expressions can solve the problem - use one of the many existing regex libraries. And if most of your problem could be solved using grep, consider writing a shell script, or using a scripting language like Python instead of a C++ program.
Your two major alternatives are (a) to use grep, or (b) to use a library, linked to your C or C++ program, which provides regular expressions.
Using grep means you get your program running very soon, because you don't have much to learn. Using a regular expression library means your program runs faster.
How much faster? The major speed increase is because you're not setting up a new process and running a new program for each of those 100 files. How significant is this speed saving?
The answer depends on how large each of those files is. If they're very large, it won't make much speed difference which method you use. If small, it will.
If you decide to go with a regular expression library, my guess is that they're all about the same speed. I chose something I was familiar with, since I know Perl: the Perl compatible regular expression library.
make forking and using exec family of command use grep and save its result in a file. in main wait for process to end. then in main open the file and use the result.
精彩评论