Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read and write file in iPhone

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

like image 273
suse Avatar asked Dec 12 '25 18:12

suse


1 Answers

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  
}  
like image 147
ennuikiller Avatar answered Dec 15 '25 12:12

ennuikiller



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!