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

The Archive Base Latest Questions

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

My code currently looks something like this (these steps splitted into multiple functions): /*

  • 0

My code currently looks something like this (these steps splitted into multiple functions):

/* open file */
FILE *file = fopen(filename, "r+");

if(!file) {

  /* read the file */

  /* modify the data */

  /* truncate file (how does this work?)*/

  /* write new data into file */

  /* close file */
  fclose(file);
}

I know I could open the file with in "w" mode, but I don’t want to do this in this case. I know there is a function ftruncate in unistd.h/sys/types.h, but I don’t want to use these functions my code should be highly portable (on windows too).

Is there a possibility to clear a file without closing/reopen it?

  • 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:31:11+00:00Added an answer on June 13, 2026 at 1:31 pm

    With standard C, the only way is to reopen the file in “w+” mode every time you need to truncate. You can use freopen() for this. “w+” will continue to allow reading from it, so there’s no need to close and reopen yet again in “r+” mode. The semantics of “w+” are:

    Open for reading and writing. The file is created if it does not exist, otherwise it is truncated. The stream is positioned at the beginning of the file.

    (Taken from the fopen(3) man page.)

    You can pass a NULL pointer as the filename parameter when using freopen():

    my_file = freopen(NULL, "w+", my_file);
    

    If you don’t need to read from the file anymore at all, when “w” mode will also do just fine.

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

Sidebar

Related Questions

My code currently looks like this: private Foo myFoo; public Foo CurrentFoo { get
My code currently looks like this: <div style=position: fixed; width: 35.25%; height: 6.75%; left:
I have an Entity in Code First Entity framework that currently looks like this:
I'm refactoring some code that currently looks like this: $$('#wrapper > div').each(function(e){ if((e.id!='header') &&
My code currently looks like this numbers = [1, 2, 3, 4, 5] def
I frequently have code that looks something like this: if (itm != null) {
I am currently working on a project where I have code that looks like
At the moment I have code to fade brightness adjustments which looks something like
I'm implementing a small HTTP server with Ruby using Mongrel. My code currently looks
This is my code currently: $(function() { $('#header_left').click(function(){ $('#header_left_info').fadeOut('fast'); $('#header_left').fadeOut('fast'); $('#header_left_back').delay(250).fadeIn('fast'); }); }); (the

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.