Objective C - Create text file to read and write line by line in Cocoa

cocoa, objective-c, text-files, xml-parsing

Solution

Well, to create a file, just use

[[NSFileManager defaultManager] createFileAtPath:@"Your/Path" contents:nil attributes:nil];

This creates an empty file, which you can write to or read from. To write text (or XML), just use `NSString`'s `writeToFile:atomically:encoding:error:` method like this

NSString *str = //Your text or XML
[str writeToFile:"Your/Path" atomically:YES encoding:NSUTF8StringEncoding error:nil];

To read from a file, just make an `NSString` with the contents of that file

NSString *contents = [NSString stringWithContentsOfFile:@"Your/Path"];

or, if it does not contain a string, get an `NSData` object from the file

NSData *contents = [NSData dataWithContentsOfFile:@"Your/Path"];

Problem

I building a Mac app,I have 2 problem: - I want to create a text file to read and write data on it. I don't know how to crate a text file to read and write data. Is it use struct? - I want to create a XML file to read and write data on it. Can I create a struct for XML? Do you have suggestion? Thanks in advance

Original source

Related problems