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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:52:18+00:00 2026-06-18T09:52:18+00:00

I have a problem with this little code for educational purposes. I can not

  • 0

I have a problem with this little code for educational purposes. I can not understand how it works.

#include <stdio.h>
#include <fcntl.h>

#define FNAME "info.txt"
#define STDIN 0
int main(){

   int fd;
   fd = open(FNAME, O_RDONLY);

   close(STDIN); //entry 0 on FDT is now free
   dup(fd); //fd duplicate is now stored at entry 0 
   execlp("more","more",0); 
}

By starting this program it prints the contents of the file “info.txt” on terminal. I can not understand why! Where is the link between “more” and STDIN (keyboard or file)?

Why if i use more with no args and without redirection on file it just shows a help screen but whit redirection it uses the file as input?

  • 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-18T09:52:19+00:00Added an answer on June 18, 2026 at 9:52 am

    dup always gives you the lowest available file descriptor number.

    By default all process will have 0, 1 and 2 for stdin, stdout and stderr. You are opening a file from that you will get a file descriptor value 3. After that you have closed stdin. Now calling dup after that will give you a lowest available value as a duplicate file descriptor for 3, so you will be getting stdin as duplicate file descriptor for 3.

    int main()
    {
       int fd, fd2;
       fd = open(FNAME, O_RDONLY); //This will be 3
    
       fd2 = dup(fd); //This will be 4 because 4 is the lowest available value
       close(STDIN); //entry 0 on FDT is now free
       dup(fd); //fd duplicate is now stored at entry 0 
       execlp("more","more",0); 
    }
    

    And here why its displaying the content of the file is, more command can be used in two ways.

    • more filename
    • command | more

    In your exec, you are not giving any filename as command line argument for more command. So its executing in pipe mode, by reading it from stdin.

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

Sidebar

Related Questions

I'm new to java script i have small problem this little code it's not
I have a little problem. This is my code: stackPanelShapesContainer = new StackPanel(); InitializeStackPanle();
This little piece of code is making me crazy: #include <stdio.h> int main() {
I have this little problem, that I cannot figure out which arguments to pass
I have this little problem. My client wanted two distinct swf on a web
I'm having a trouble with this little problem. Let's say, I have an array,
Alright, this problem is a little complicated, so bear with me. I have a
I have problem with this code: file = tempfile.TemporaryFile(mode='wrb') file.write(base64.b64decode(data)) file.flush() os.fsync(file) # file.seek(0)
Hi i have problem with this code, i found it on the internet and
i am a beginner and i have a problem : this code doesnt compile

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.