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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T05:54:26+00:00 2026-06-04T05:54:26+00:00

ios claims the file has been written to, but the changes never actually save,

  • 0

ios claims the file has been written to, but the changes never actually save, as if they remain in buffer. Do I have to flush or something?

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSString *myFilePath = [[NSBundle mainBundle]
                            pathForResource:@"MyFile"
                            ofType:@"txt"];

    NSLog(@"\n\nPath = \n%@", myFilePath);

    NSString *myFileContents = [NSString stringWithContentsOfFile:myFilePath
                                    encoding:NSUTF8StringEncoding
                                    error:nil];


    NSLog(@"\n\nContents = \n%@", myFileContents);

    [@"This line will be written to file" writeToFile:myFilePath
        atomically:YES encoding:NSUTF8StringEncoding error:nil];

    NSString *changedContents = [NSString stringWithContentsOfFile:myFilePath
                                    encoding:NSUTF8StringEncoding
                                    error:nil];

    NSLog(@"\n\nChanged Contents = \n%@", changedContents);
}

Output:

Path = 
/Users/hamzeh/Library/Application Support/iPhone Simulator/5.1/Applications/2B318687-A745-4CD9-85BA-52A01AB76F6E/savepersistentdata_tutorial.app/MyFile.txt

Contents = 
This is a text file.
The file will be displayed on the iPhone.
That is all for now. 

Changed Contents = 
This line will be written to file

This is the output I get every time I run, so the changes don’t actually get written to the file.

Thanks for the help.

  • 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-04T05:54:27+00:00Added an answer on June 4, 2026 at 5:54 am

    You can’t edit files in your main bundle. You have to first save the file to the applications documents folder then make any changes.

    Here’s some code to maybe fix your issue:

    First save the text to a directory you create:

    NSString *myFilePath = [[NSBundle mainBundle]
                                pathForResource:@"MyFile"
                                ofType:@"txt"];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
    NSString *directory = [NSString stringWithFormat:@"%@/Some_Directory", [paths objectAtIndex:0]];
    
    // Check if the directory already exists
    if (![[NSFileManager defaultManager] fileExistsAtPath:directory]) {
        // Directory does not exist so create it
        [[NSFileManager defaultManager] createDirectoryAtPath:directory withIntermediateDirectories:YES attributes:nil error:nil];
    } 
    
    NSData *data = [[NSData dataWithContentsOfFile:myFilePath] retain];
    
    NSString *filePath = [[directory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@", [myFilePath lastPathComponent]]] retain];
    
    
    
    NSError *error = nil;
    if (![data writeToFile:filePath options:NSDataWritingFileProtectionNone error:&error]) {
        [data writeToFile:filePath atomically:NO];
    }
    [data release];
    [filePath release];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

iOS 4 has an In-App SMS SDK for sending messages, but is there a
My IOS application program have tableview in navigation controller secondview. I want to save
iOS beginner here. I'm using the following code to save my facebook accessToken and
iOS has had high speed scrubbing in the media player since iOS3. However, the
Apparently iOS 6 tries to automatically handle the situation when you have a gesture
Many iOS developers have found the Cocoa Lumberjack Logging framework to fill a need
I've been using web-socket-js for websocket support across devices. Works great in all IOS
iOS 5's storyboard is awesome but I've read that it would be better to
iOS 5.0 SDK I have a method that took a parameter as a 'type'
iOS 6, iPhone 5, Xcode 4.5 I have an app that let users download

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.