views:

74

answers:

1

Hello,

How to make file read/write operation on iPhone?which is the path i need to specify to save the file? how can i get the current working directory in iPhone?

Thank You

+1  A: 

Write to a file:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

// the path to write file
NSString *myFile = [documentsDirectory stringByAppendingPathComponent:@"myFile"];

[data writeToFile:myFile atomically:YES];

Read a file:

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"MyFile" ofType:@"txt"];  
NSData *myData = [NSData dataWithContentsOfFile:filePath];  
if (myData) {  
    // do something useful  
}  
ennuikiller
how to check if the file is written on iPhone device or not?
suse
http://developer.apple.com/iphone/library/documentation/cocoa/reference/foundation/Classes/NSData_Class/Reference/Reference.html#//apple_ref/occ/instm/NSData/writeToFile:options:error: `writeToFile` returns a `YES` or `NO` status.
ohho