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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:40:54+00:00 2026-05-26T06:40:54+00:00

What is the most efficient quickest way to write all zeros to a file?

  • 0

What is the most efficient quickest way to write all zeros to a file? including error checking. Would it just be fwrite? or is fseek involved?

I’ve looked elsewhere and saw code similar to this:

off_t size = fseek(pFile,0,SEEK_END);
fseek(pFile,0,SEEK_SET);

while (size>sizeof zeros)  
    size -= fwrite(&address, 1, sizeof zeros, pFile); 
while (size)    
    size -= fwrite(&address, 1, size, pFile); 

where zeros is an array of file size I suspect. Not sure exactly what off_t was because it wasn’t directly intuitive to me anyways

  • 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-26T06:40:54+00:00Added an answer on May 26, 2026 at 6:40 am

    Do you want to replace the contents of the file with a stream of binary zeroes of the same length, or do you want to simply empty the file? (make it have length zero)

    Either way, this is best done with the OS file I/O primitives. Option one:

    char buf[4096];
    struct stat st;
    int fd;
    off_t pos;
    ssize_t written;
    
    memset(buf, 0, 4096);
    fd = open(file_to_overwrite, O_WRONLY);
    fstat(fd, &st);
    
    for (pos = 0; pos < st.st_size; pos += written)
        if ((written = write(fd, buf, min(st.st_size - pos, 4096))) <= 0)
            break;
    
    fsync(fd);
    close(fd);
    

    Option two:

    int fd = open(file_to_truncate, O_WRONLY);
    ftruncate(fd, 0);
    fsync(fd);
    close(fd);
    

    Error handling left as an exercise.

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

Sidebar

Related Questions

What is the quickest most efficient way to search text for words using non-casesensitive
What would be the most efficient way of recording to a log (.txt) from
What would be the most efficient way to read a UInt32 value from an
I was wondering what the quickest, easiest and most efficient way of running some
What's the quickest and most efficient way of reading the last line of text
What is the most efficient way of checking if an environment variable has been
What is the quickest and most efficient way of finding a string within another
What is the most efficient (performant) way to create a list with just one
what is the most efficient way to return all embedded documents? say a User
I have an NSURL: serverCall?x=a&y=b&z=c What is the quickest and most efficient way to

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.