Search code examples
c++commandsystemwcexecve

running multiple execve functions in one c++ file


I have to write a c++ program which "counts the number of lines, words and the number of bytes from a text file", all of which must be in a new line.

I have to use the wc command in my c++ program. I have managed to get the number of lines:

char *envp[] = {NULL};
char *command[] = {"wc", "-l", filename.c_str(), NULL};
execve("/usr/bin/wc", command, envp);

After the above statements, I have one which replaces "-l" with "-w" and so forth. But my program ends immediately after the first execve() statement.

How do I get all my statements to execute even after the execve() statement?

N.B: This will be my first time running system commands using a c++ program.

Thank you in advance.


Solution

  • execve replaces current executable image with a specified one and therefore never returns upon success. If you want to continue executing main program then you will need to fork first. Or use something as dull as system function.