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
iOS 5's Mail app has a nifty little swipe gesture that brings up the
iOS apps will have a class that adopts the UIApplicationDelegate, called something like xxxAppDelegate.
My iOS app has a need to uniquely identify each device so it can
(iOS) I have a UITableView as a root view (screen 1). when a cell
(iOS) I have a view that contains multiple textviews under static headings (see image
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'
In iOS I have an app where I need to wait for the currently
iOS UIScreen has brightness property. Setting it to a different value updates screen brightness.

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.