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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T01:51:49+00:00 2026-06-04T01:51:49+00:00

some functions to write files are atomic and therefore quite convenient in the sense

  • 0

some functions to write files are atomic and therefore quite convenient in the sense that they prevent file corruption should something happen at write-time.

-[NSData writeToFile:atomically:]
-(BOOL)writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFile encoding:(NSStringEncoding)enc error:(NSError **)error;

the problem is that they erase the file and replace it with the new content… And I need to just append one line to a huge file.

What is the best way to do that in an atomic way, not risking to corrupt that file should something happen?

PS: the file is too huge to read it in one string, update the string and then push the enormous string to the file system.

Thanks in advance.

  • 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-04T01:51:50+00:00Added an answer on June 4, 2026 at 1:51 am

    The reason there is no function like that is because the atomic versions makes a copy of the file, writes everything to it and then renames the new file to the same name as the old one and finally removes the old file. As such the original file is actually never modified but rather replaced with a new file.

    If you want atomic appends that are fast, you can use fwrite and fsync to get the acheived effect. fwrites that are for less than PIPE_BUF (4096 bytes on iOS) followed by fsync are guaranteed to be atomic.

    Here is a short snippet for a category that will do the operation, note that it misses proper error-checking code for the syscalls.

    @implementation NSData(AppendAtomically)
    
    - (void)appendToFileAtomic:(NSString *)filePath
    {
        NSAssert([self length] < PIPE_BUF, @"Cannot write messages longer than %d atomically", PIPE_BUF);
    
        const char *utfpath = [filePath UTF8String]; 
    
        FILE *f = fopen(utfpath, "ab");
        fwrite([self bytes], 1, [self length], f);
        fsync(fileno(f));
        fclose(f);
    }
    
    @end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a file Tools.ml which contains some common utility functions I write myself.
I'd like to write a function that would have some optional code to be
I need to write a delegate function that can 'wrap' some while/try/catch code around
I'm trying to write a javascript function that adds some DOM nodes to the
I'm trying to write a recursive function that would get some string, as well
I've got a function I wrote quite some time ago that works fine, but
I have to write some member functions of a class in Assembly for an
I'm trying to write a macro that will generate a plain-text list of files
I'm trying to write some simple code that will return the directory for the
One of my favorite features about python is that you can write configuration files

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.