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 7990779
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T13:06:46+00:00 2026-06-04T13:06:46+00:00

I have the following c code. I want to display my file with less

  • 0

I have the following c code. I want to display my file with less by calling execv()
however the following seems never work. The program terminates and noting pop out.

#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
int main(void){
  int pid; 
  if(pid=fork()>0){
      //read in from stdin and pass to pipe
   }else if(pid==0){
      //read from pipe
      //write to out.txt
      //everything up to here works fine

      char* para[]={"less","/Desktop/out.txt"};
      execv("/bin/less",para);
   }
   return 0;
}
  • 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-04T13:06:48+00:00Added an answer on June 4, 2026 at 1:06 pm

    (The original code contained execv("bin/less", para);.) Unless the current directory is the root directory, /, or unless there is a program less in the subdirectory ./bin/less, then one of your problems is that you have a probable typo in the name of the executable. That assumes the program is /bin/less and not /usr/bin/less. You might even use execvp() to do a PATH-based search for the program.

    There’s an additional problem: you need to include a null pointer to mark the end of the argument list.

    Finally, you can print an error message after the execv() returns. The mere fact that it returns tells you it failed.

    #include <stdlib.h>
    #include <unistd.h>
    #include <stdio.h>
    
    int main(void)
    {
        int pid;
        if ((pid = fork()) != 0)
        {
            // read in from stdin and pass to pipe
            // Need to test for fork() error here too
        }
        else
        {
            // read from pipe
            // write to out.txt
            // everything up to here works fine
    
            char *para[] = { "/bin/less", "Desktop/out.txt", 0 };
            execv(para[0], para);
            fprintf(stderr, "Failed to execute %s\n", para[0]);
            exit(1);
        }
        return 0;
    }
    

    Or:

            char *para[] = { "less", "Desktop/out.txt", 0 };
            execvp(para[0], para);
            fprintf(stderr, "Failed to execute %s\n", para[0]);
    

    The remarks in the code about pipes are puzzling since there is no sign of pipes other than in the comments. As it stands, less will read the file it is told to read. Note that less will not paginate its output if the output is not going to a terminal. Since we can see no I/O redirection, we have to assume, then, that less will ignore anything the program tries to write to it, and will not send any data back to the program.

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

Sidebar

Related Questions

I have a following code , I want to display all the attributes of
I have the following code snippet from a HTML file: <div id=rwImages_hidden style=display:none;> <img
I have the following code generated by Eclipse (.java file). import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Display;
I have following code and i want to pass additional parameter in JSON.getJSONfromURL() method
I have the following code I want to run, but the problem is $this->type
I want to know is below code correct ? I have following code which
I have the following code, which I want to load values on a selection
I have the following code: foreach (var control in this.Controls) { } I want
I have the following code where after a bool is true I want to
I have the following code; I want to make sure that submit does not

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.