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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:40:59+00:00 2026-05-22T23:40:59+00:00

I am modifying a big C code. For test purposes I had to redirect

  • 0

I am modifying a big C code. For test purposes I had to redirect stdout to a file. I used this code snipped for this purpose: fp=freopen("OUT", "w" ,stdout) Now all printf calls will write to fp. It is a big code so I do not want to search all of the exit points and close the file before each exit. What happens if do not close the file? Is there a way to make it autoflush each time I write something to the file?

  • 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-22T23:41:00+00:00Added an answer on May 22, 2026 at 11:41 pm

    setvbuf is probably the way to go:

    setvbuf (fp, NULL, _IONBF, 0);
    

    This will turn off buffering altogether. Just be prepared for the inevitable performance hit.

    You also need to be aware that this will flush only at the runtime-library level in many systems, it will not necessarily cause a flush to the storage medium, as the UNIX fsync(fileno(fp)) would try to do.

    So, while it will be okay if your program crashes, it will not help if the whole OS falls in a screaming heap. But then you probably have bigger problems than losing a little bit of output 🙂


    But, unless your program is crashing, you probably shouldn’t worry about it. ISO C99 says, in part, that one of the actions of exit(), and hence returning from main(), is:

    Next, all open streams with unwritten buffered data are flushed, all open streams are closed, …

    So your data will be output regardless in that case.

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

Sidebar

Related Questions

This is the first time I've used NHibernate for a big project so bear
I am modifying some code and came across a declaration that I am having
I am modifying a SQL table through C# code and I need to drop
I'm modifying existing security code. The specifications are pretty clear, there is example code,
We all know that modifying a .NET web application's web.config file restarts the app
I am currently modifying a piece of code and I am wondering if the
I feel like this is a silly question but I always find that modifying
This is a design question, concrete code not submitted to protect my bottom. When
When modifying code I find I often have to go through dozens of files
According to my small tests this code works. But, does it have undefined behaviour?

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.