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

  • SEARCH
  • Home
  • 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 9305941
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T00:03:10+00:00 2026-06-19T00:03:10+00:00

I have the following code: int main(void) { char in[100]; printf(> ); fgets(in, sizeof(in),

  • 0

I have the following code:

int main(void) {
    char in[100];
    printf("> ");
    fgets(in, sizeof(in), stdin);
    int pid;
    char *f[100];
    char *v;
    int i = 1;
    v = strtok(in, " ");
    f[0] = v;
    while (v = strtok(NULL, " ")){
        f[i] = v;
        i++;
    }
    f[i] = NULL;
    execvp(f[0], f);
    perror("FAILED");
    exit(1);
}

For some reason, anytime I enter in something like “ls -l” or “date”, it tells me no such file or directory. When I wrote in the code:

f[0] = "ls";
f[1] = "-l";
f[2] = NULL;

it worked fine. Any ideas?

  • 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-19T00:03:12+00:00Added an answer on June 19, 2026 at 12:03 am

    There is a small mistake in your code. This is about the usage of the fgets function.
    When you use fgets the whole given as input along with the new line character is taken into the character string v. Just add the following two lines after fgets function and your code works fine.

    int size = strlen(in);
    in[size-1] = '\0';
    

    This is just to remove the new line character from the string.

    So when you press ls -l[ENTER]. ls is taken in the first word f[0] and -l’\n’ is taken in f[1] and the execvp is not able to find and executable with ls -l’\n’

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

Sidebar

Related Questions

I have the following code: int get_int(void) { char input[10]; fgets(input, 10, stdin); //
I have the following code: #include <string.h> int main(void) { char *buffer = NULL,
For example, I have the following code: #define MAX_CLIENTS 100; void main() { char*
So...I have the following code: int main(void) { const char *s=hello, world; cout<<&s[7]<<endl; return
Lets look at the following code: int main(void) { char c; while ((c =
I have the following code int main() { int a=6; void *p; p=&a; p++;
i have the following code: #include <stdio.h> int main(void) { float a[4] __attribute__((aligned(0x1000))) =
I have the following code: int main(int argc, char** argv) { onelog a; std::cout
I have the following code: template<int k> void foo() { } int main(int argc,
Suppose you have the following code: int main(int argc, char** argv) { Foo f;

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.