Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7887535
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T05:36:21+00:00 2026-06-03T05:36:21+00:00

I am trying to exec() a call to grep inside of my C program.

  • 0

I am trying to exec() a call to grep inside of my C program. I am looking for the number of occurrences of a string pattern in a given file, which can be achieved with:

$ grep -e my_string -F my_file.txt -c

Which gives a number output as expected, such as:

5

However, what I hoped would be equivalent seems to be having some difficulties. The following code:

char *arg_list[] = {"-e", "my_string", "-F", "my_file.txt", "-c", "\0"};

int pid;
if ((pid = fork()) == 0) {
    if (execvp("grep", arg_list) < 0)
        perror("execvp");
}

Results in:

 my_file.txt:5
 -e: : No such file or directory

There are two issues. First, I just want the number– not the file in which it is found. Second, I’m not sure why it is trying to assert that the -e flag needs to be a file. If I switch around the argument list to be:

char *arg_list[] = {"-F", "my_file.txt", "-e", "my_string", "-c", "\0"};

Then I get back:

my_file.txt:5
-F: : No such file or directory

I’m not sure where to start looking for why this is happening. I’m also not sure why the output of the grep call from the command-line is just a number, but the exec()’d grep call outputs both the filename and number.

Any help would be appreciated! Thank you.

EDIT:

Solution: include the program name in the argument list, or simply use the system() call. Also, RTFM.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T05:36:22+00:00Added an answer on June 3, 2026 at 5:36 am

    You have two problems (insert JWZ quote about regular expressions here):

    1. The first argument in the array passed exec gets passed in as argv[0] to the new executable. Ordinarily, this is the name of the executable, and it’s what programs use to figure out their own name. So you’re telling grep that its name is -e, and its arguments are my_string -F my_file.txt -c "". It’s not getting the -e argument like you might expect, so it misinterprets its arguments and gives you an error.

    2. You’re not properly NULL-terminating the array of arguments to exec. The last argument must be a literal NULL, not a string "\0". That string is an empty string, but it’s not a null pointer.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to dynamically call a method given in a string using parameters given
i'm trying to write a php page that call for a server program like
I am trying to call the sccmap command from GraphViz using Java. String command
I'm trying to call JDIC from my application, but I can't get it to
I'm trying to run ffmpeg through a PHP exec call, I've been debugging for
I'm trying to play a video file during a call with asterisk 1.4 for
I'm trying to use Runtime.exec to start a copy of the current process. I
I'm trying out the Runtime.exec() method to run a command line process. I wrote
I am trying to run this code : exec myStoredProcedure Cast('c5b48202-36af-4597-9780-5366d4188f55' AS uniqueidentifier), 744,
I am trying to spawn a process using Runtime.exec. I want to use my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.