user987723
user987723

Reputation: 965

writing string to txt file in objective c

Pulling my hair out trying to work this out. i want to read and write a list of numbers to a txt file within my project. however [string writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:&error] doesnt appear to write anything to the file. I can see there is the path string returns a file path so it seems to have found it, but just doesnt appear to write anything to the file.

+(void)WriteProductIdToWishList:(NSNumber*)productId {

    for (NSString* s in [self GetProductsFromWishList]) {
        if([s isEqualToString:[productId stringValue]]) {
            //exists already
            return;
        }
    }

    NSString *string = [NSString stringWithFormat:@"%@:",productId];   // your string
    NSString *path = [[NSBundle mainBundle] pathForResource:@"WishList" ofType:@"txt"];
    NSError *error = nil;
    [string writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:&error];
    NSLog(@"%@", error.localizedFailureReason);


    // path to your .txt file
    // Open output file in append mode: 
}

EDIT: path shows as /var/mobile/Applications/CFC1ECEC-2A3D-457D-8BDF-639B79B13429/newAR.app/WishList.txt so does exist. But reading it back with:

NSString *path = [[NSBundle mainBundle] pathForResource:@"WishList" ofType:@"txt"];

returns nothing but an empty string.

Upvotes: 3

Views: 8572

Answers (2)

ott--
ott--

Reputation: 5722

To escape from all the hassle with writing to a file in a specific directory, use the NSUserDefaults class to store/retrieve a key-value pair. That way you'd still have hair when you're 64.

Upvotes: 2

isaac
isaac

Reputation: 4897

You're trying to write to a location that is inside your application bundle, which cannot be modified as the bundle is read-only. You need to find a location (in your application's sandbox) that is writeable, and then you'll get the behavior you expect when you call string:WriteToFile:.

Often an application will read a resource from the bundle the first time it's run, copy said file to a suitable location (try the documents folder or temporary folder), and then proceed to modify the file.

So, for example, something along these lines:

// Path for original file in bundle..
NSString *originalPath = [[NSBundle mainBundle] pathForResource:@"WishList" ofType:@"txt"];
NSURL *originalURL = [NSURL URLWithString:originalPath];

// Destination for file that is writeable
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSURL *documentsURL = [NSURL URLWithString:documentsDirectory];

NSString *fileNameComponent = [[originalPath pathComponents] lastObject];
NSURL *destinationURL = [documentsURL URLByAppendingPathComponent:fileNameComponent];

// Copy file to new location
NSError *anError;
[[NSFileManager defaultManager] copyItemAtURL:originalURL
                                        toURL:destinationURL
                                        error:&anError];

// Now you can write to the file....
NSString *string = [NSString stringWithFormat:@"%@:", yourString]; 
NSError *writeError = nil;
[string writeToFile:destinationURL atomically:YES encoding:NSUTF8StringEncoding error:&error];
NSLog(@"%@", writeError.localizedFailureReason);

Moving forward (assuming you want to continue to modify the file over time), you'll need to evaluate if the file already exists in the user's document folder, making sure to only copy the file from the bundle when required (otherwise you'll overwrite your modified file with the original bundle copy every time).

Upvotes: 10

Related Questions