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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:54:21+00:00 2026-06-13T13:54:21+00:00

The following code is expected to write some text to demo.txt, but it doesn’t

  • 0

The following code is expected to write “some text” to demo.txt, but it doesn’t work:

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>

int main(int argc, char *argv[])
{
    FILE *fp;
    int fd;

    if ((fd = open("demo.txt", O_RDWR)) == -1) {
        perror("open");
        exit(1);
    }
    fp = fdopen(fd, "w");
    fprintf(fp, "some text\n");
    close(fd);
    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-13T13:54:22+00:00Added an answer on June 13, 2026 at 1:54 pm

    You should use fflush(fp) to clear the buffer before closing the file.

    When you write to the file descriptor fp, the data is buffered.
    But you are closing the file with close(fd) before the buffered data can be written written to the file demo.txt. Hence, buffered data is lost.
    If you do fflush(fp), it will ensure the buffered data is written to demo.txt immediately.

    You should not call close() before doing fclose() for all the opened files.

    Proper way is to do fclose(fp) first and then do close(fd).

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

Sidebar

Related Questions

For some reason the following code does not work as expected when in IE
The following code works as expected on CentOS and Ubuntu O/s but not on
The following code is throwing two alerts as expected in IE but not in
In the following code, the first log statement shows a decimal as expected, but
Following chunk html code works as expected: <iframe src=http://www.amazon.com/></iframe> But when trying embed inner
The following code doesn't appear to work properly for me. It requires starting a
The following code basically works as expected. However, to be paranoid, I was wondering,
The following code fails in 'Evaluate' with: This expression was expected to have type
When I run the following code I get B as expected: class A {
The following code converts a PDF page into a JPEG. It runs as expected

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.