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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T13:25:53+00:00 2026-06-10T13:25:53+00:00

I am looking at I/O operations in C++ and I have a question. When

  • 0

I am looking at I/O operations in C++ and I have a question.
When opening a file like:

#include <fcntl.h>
int main() {
    unsigned char buffer[16];     
    int fd = open (argv[1], O_RDONLY);
    read(fd, buffer, sizeof(buffer));
    return 0;
}

How can the variable fd represent a file as an integer when passing it to the open method? Is it repesenting a file in current folder? If I print the ´fd´variable, it prints 3. What does that mean?

Ps. I know there are several other ways to handle files, like stdio.h, fstream etc but that is out of the scope of this question. Ds.

  • 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-10T13:25:55+00:00Added an answer on June 10, 2026 at 1:25 pm

    How can the variable fd represent a file as an integer when passing it to the open method?

    It’s a handle that identifies the open file; it’s generally called a file descriptor, hence the name fd.

    When you open the file, the operating system creates some resources that are needed to access it. These are stored in some kind of data structure (perhaps a simple array) that uses an integer as a key; the call to open returns that integer so that when you pass it read, the operating system can use it to find the resources it needs.

    Is it repesenting a file in current folder?

    It’s representing the file that you opened; its filename was argv[1], the first of the arguments that was passed to the program when it was launched. If that file doesn’t exist, or open failed for some reason, then it has the value -1 and doesn’t represent any file; you really should check for that before you try to do anything with it.

    If I print the fd variable, it prints 3. What does that mean?

    It doesn’t have any particular meaning; but it has that value because it was the fourth file (or file-like thing) that was opened, after the input (0), output (1) and error (2) streams that are used by cin, cout and cerr in C++.

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

Sidebar

Related Questions

As a recent question hinted, I'm looking for a way to speed up operations
I am looking to get into operating system kernel development and figured and have
I am looking for a way that I can do bitwise operations (for crypto
I'm looking for a elaborate list comparing different operations in PHP. For example: echo
Looking to do a bit of refactoring... Using NHibernate I have this query currently
I have not used PackedArray before, but just started looking at using them from
I have alluded to this issue in my other question , but i think
I do have couple of solutions(rather call them hacks) for doing this but looking
I have been looking for a solution to this problem for a while now
We have a working WPF app that we are looking into running in the

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.