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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:04:12+00:00 2026-06-11T10:04:12+00:00

I was analyzing the way a C program in Linux writes to a file

  • 0

I was analyzing the way a C program in Linux writes to a file and I summarize it to:

#include <fcntl.h>
#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>
#include "restart.h"
#define CREATE_FLAGS (O_WRONLY | O_CREAT | O_APPEND)
#define CREATE_MODE (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)

int main(void) {

   int fd;

   fd = open("my.file", CREATE_FLAGS, CREATE_MODE);

   dup2(fd, STDOUT_FILENO);

   r_close(fd) ;

   write(STDOUT_FILENO, "Hello World", 11) ;

   return 0;
}

the two small header files r_close.c and restart.h are in THIS link.

So my question is I will like to know how the program is writing to the file. For example what is the difference of doing it in c# as:

    static void Main(string[] args)
    {
        var file = System.IO.File.OpenWrite("my.file");
        file.Write(System.Text.Encoding.ASCII.GetBytes("Hello World"), 0, 11);
        file.Close();
    }

I looked at the description of dup2 method in many links such as at http://www.mkssoftware.com/docs/man3/dup2.3.asp and I am having a hard time trying to understand what it basically does.

  • 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-11T10:04:14+00:00Added an answer on June 11, 2026 at 10:04 am

    The dup2() function duplicates an open file descriptor. Specifically,
    it provides an alternate interface to the service provided by the
    fcntl() function using the F_DUPFD constant command value, with
    fildes2 for its third argument. The duplicated file descriptor shares
    any locks with the original.

    The call:

    fid = dup2(fildes, fildes2);
    

    is equivalent to:

    close(fildes2);
    fid = fcntl(fildes, F_DUPFD, fildes2);
    

    What this means is that we have modified the table that the OS has to hold the open files descriptor. It uses the fcntl() function. fcntl() provides control of open file descriptors. Basicly it copies an int to an int. But it does it using a system call; as it modifies an OS resource. Your saying to the system: You know that Index in the FileTable? Yes? well copy the FilePointer to another Index. ( A few other things also but I’ll have to ignore that for now.)

    Re: the second part of your question, What is the difference?

    They basically do the same thing. Probably exactly the same chars are written to the file. It does do one extra thing. That is it dup2() STDOUT_FILENO. to a file. That changes what the standard output is. If I’m not mistaken that code will redirect all output that would normally go to the console to fd, in your case "my.file". That’s the main difference between the C# and C done you listed.

    It should also be noted that the whole dup2() in this sample is kind of pointless if all you are concerned with is to write “Hello World” to a file. It is sorta like going int x = 5; int y = x; Console.WriteLine(y);. The extra step doesn’t anything in your example.

    Another, more general difference, is that C# hides a-lot of the low level stuff from you the programmer. While coding in C, you will be able to do lower level file and system manipulation with more ease. The downside is that you have to do more coding of the lower level stuff. While mostly you don’t really care exactly how that stuff happens, rather that it does indeed happen. So often, it’s quicker to code in C# because all the low level stuff is handled nicely for you already. — Sorta off topic point that may help you understand

    Finally I will hazard a guess that;

    I was analyzing the way a C program in Linux writes to a file and I
    summarize it to:

    You were analyzing a program that deals with I/O manipulation, rather than ordinary writing to a file. That would explain the dup2().

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

Sidebar

Related Questions

Is there any way how to call JQuery function from PageLoad C# file? Basically
I'm analyzing data with Apache pig and could not find a way to expand
My program is analyzing big XML files and producing a report based on the
I am creating a program for analyzing and generating queries. I was curious if
I am writing a program for analyzing certain type of packets. I got the
I'm a business major, two-thirds of the way through my degree program, with a
I am analyzing timestamped YouTube comments. Because some comments may refer to a period
I am analyzing some experimental data in the form of .tiff multi frames. Withins
I was analyzing a code and I am confused on a particular code. I
I'm analyzing the different behaviors between the JTA (Java Transactions API) and the .NET

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.