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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T03:23:18+00:00 2026-06-11T03:23:18+00:00

To make the question more clear: [case 1] #include <stdio.h> int main () {

  • 0

To make the question more clear:

[case 1]

#include <stdio.h>
int main ()  {
    FILE* file = fopen("myfile.txt", "r");
    return 0;
}

[case 2]

#include <stdio.h>
int main () {
    int fd = open("myfile.txt", O_RDONLY);
    return 0;
}

What happens when the file is not properly closed on both examples? Are the buffers flushed into the files? I am assuming the files will at least be closed automatically… is that right?

  • 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-11T03:23:19+00:00Added an answer on June 11, 2026 at 3:23 am

    fopen is a C library function. It is unspecified how it is implemented. [Correction:] If you return from main, all open file streams are closed for you (e.g. C11 7.21.3/5), but this need not happen if the program exits in other ways (e.g. via signal, such as via abort).

    Your underlying OS implementation will of course clean up everything properly when a process terminates, but that’s only in as far as the global state of your system is concerned (e.g. all virtual memory associated to your process will be released, and all file descriptors closed, etc.). If the C library implements its own write buffer, say, then there’s no reason that that would automagically get flushed in every situation (though normal exit by returning from main is fine).

    That also addresses your case 2: If you use OS features directly, like the Posix open call, then you are indeed assured that those file descriptors will be closed when your process exits.

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

Sidebar

Related Questions

Ok, I'm going to try to make this more clear because my last question
Someone please edit my title to make this question more clear. It's easier to
Edited to make the question more clear. I am trying to work with Isolates
Ok, so I'll try to make that question a little more simple. The core
I am editing the question to make it more specific. I want to run
I edited the question so it would make more sense. I have a function
NB: This question has been extensively edited to make it more relevant, for completeness
This is a long question. I've tried to make it more concise but I
I've deleted my original question regarding this and reposted to make this question more
This is more of a specific question on a specific case. Here is what

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.