File input/output: Difference between revisions

Use more relevant example.
(Whitespace, comments)
(Use more relevant example.)
Line 595:
[[Category:GNU Compiler Collection]]
 
Read the contents of input.txt and place it in output.txt, creating a file if needed:
Many objects know how to create an instance from a file or write an instance to file. In this example, a dictionary write itself to a file using XML property list format, and then read back into a new instance.
 
NSData *input = [NSData dataWithContentsOfFlie:@"input.txt"];
NSDictionary *info = [NSDicionary dictionaryWithObjectsAndKeys:
[infodata writeToFile:@"/tmp/testoutput.plisttxt" atomically:YES];
@"name", @"Joe",
@"age", [NSNumber numberWithUnsignedInt:37],
nil];
// Write to file
[info writeToFile:@"/tmp/test.plist" atomically:YES];
// Read from file
NSDictionary *saved = [NSDictionary dictionaryWithContentsOfFile:@"/tmp/test.plist"];
 
Displayed without error checking to make it more clear. In real code you will need to add lot of error checking code, and maybe use dataWithContentsOfFile:error: if you want to get error information on failure.
There are many other methods to read and write files, accessing file attributes etc.
 
==[[Perl]]==
Anonymous user