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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:17:41+00:00 2026-05-27T07:17:41+00:00

Here is my code: #include<stdio.h> #include<stdlib.h> #include<sys/stat.h> #include<sys/types.h> #include<fcntl.h> #include<unistd.h> #include<errno.h> int main(int argc,char

  • 0

Here is my code:

#include<stdio.h>
#include<stdlib.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<fcntl.h>
#include<unistd.h>
#include<errno.h>
int main(int argc,char *argv[])
{
  int oldfd;
  int newfd;
  if(argc!=2)
  {
    printf("Usgae : %s file_name\n",argv[0]);
    exit(0);
  }
 oldfd=open(argv[1],O_RDWR|O_APPEND,S_IRWXU); // Opening the file in Read/Write mode
 if (-1 == oldfd)
 {
  perror("Error opening file");
  exit(0);
 }
 close(1); // closing stdout 
 newfd=dup(oldfd); //Now this newfd holds the value 1 
 close(oldfd); //closing the oldfd
 printf("\nStack Overflow"); //Now this printf will print content into the file as stdout closed already
 close(newfd);// closing newfd 
 return 0;
}

What I’m actually trying to do is just printing “Stack Overflow” to the file using printf() instead of write() system call.

Its not printing the content into the file. But one thing i observed is that if i remove the code:

 close(newfd)

It’s printing the contents into the file as expected. But I can’t understand why. I printed the contents and then only I’m closing the newfd.

Whats the reason for this?

  • 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-05-27T07:17:42+00:00Added an answer on May 27, 2026 at 7:17 am

    What’s actually happening here is that printf‘s output is buffered and not sent to fd 1 immediately; instead the buffer is flushed to file descriptor 1 by the C run-time after you return from main. If you close(newfd), you’ve effectively obstructed the automatic flush that is otherwise performed by the run-time at exit.

    If you explicitly fflush(stdout) before you close(newfd), your output should appear in the file.

    By the way, if you want to redirect a specific file descriptor, there is an alternate system call dup2(oldfd, 1) which makes fd 1 a duplicate of oldfd, closing fd 1 if it was previously open.

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

Sidebar

Related Questions

see my code is #include <stdio.h> #include<stdlib.h> #include<sys/stat.h> int main(int argc, char**argv) { unsigned
Here is my code for fork() system call, #include<stdio.h> #include<stdlib.h> #include<sys/types.h> #include<sys/stat.h> #include<unistd.h> #include<errno.h>
Here is my code: #include <stdio.h> #include <stdlib.h> #include <dirent.h> #include <string.h> #include <sys/stat.h>
Here is my code: #include <stdio.h> int main(void) { FILE *fp; unsigned int i;
Here's a simple C file: #include <stdio.h> #include <stdlib.h> int main() { printf(hi there!\n);
Here is my code which checks if the file exists : #include<stdio.h> #include<zlib.h> #include<unistd.h>
Here is my code: #include <iostream> #include <stdlib.h> #include <fstream> using namespace std; int
Here's my code so far: #include<iostream> #include<string> #include<fstream> using namespace std; int main() {
I am going to paste here my code and errors: #include stdio.h #include winsock2.h
Here's the code. Not much to it. <?php include("Spreadsheet/Excel/Writer.php"); $xls = new Spreadsheet_Excel_Writer(); $sheet

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.