2

Using URLConnection I am downloading a file from a url and storing that data to file in documents directory. When I receive response, when the didReceiveResponse: method called, I create file and move to end of that file this way:

NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:self.fileName];
NSLog(@"filepath is %@",filePath);
[[NSFileManager defaultManager] createFileAtPath:filePath contents:nil attributes:nil];
file = [[NSFileHandle fileHandleForUpdatingAtPath:filePath] retain];
[file seekToEndOfFile];

where File is object of NSFileHandle class. Later while receiving data, whenever the didReceiveData:(NSData *)data method called, I store the received data to the file this way:

[file seekToEndOfFile];
[file writeData:data];

Finally I close the file in connectionDidFinishLoading: method this way:

[file closeFile];

It was working when I build and run in my system. But when I copied to another system and tried to build I get this error :

Terminating app due to uncaught exception `'NSFileHandleOperationException'`, reason: '*** `-[NSConcreteFileHandle seekToEndOfFile]: No such file or directory'`

I refered http://www.techotopia.com/index.php/Working_with_Directories_on_iPhone_OS and What is NSSearchPathForDirectoriesInDomains? for knowing about NSSearchPathForDirectoriesInDomain function but I am still not clear.

Will UserDomainMask return the proper path depending on system and user in which the project exists. If so why I am getting such error?

They say the path returned (stored in filepath) will be the path /Users//Library/Application Support/iPhone Simulator/User/Application//Documents where

is the name of the user currently logged into the Mac OS X system on which the simulator is running and is the unique ID of the app

Can anyone help me, to understand why I am getting such error.

Community
  • 1
  • 1
aparna
  • 353
  • 2
  • 3
  • 13
  • I have changed the question and added few code where the NSSearchFilePathInDirectoriesForDomain function is used – aparna May 15 '13 at 10:47

2 Answers2

7

As On your Requirment No Source code and you showing the Exception so we have these path for wrting and reading. and share what you using ?

Documents

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

Library

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

cache directory

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cachePath = [paths objectAtIndex:0];
BOOL isDir = NO;
NSError *error;
if (! [[NSFileManager defaultManager] fileExistsAtPath:cachePath isDirectory:&isDir] && isDir == NO) {
    [[NSFileManager defaultManager] createDirectoryAtPath:cachePath withIntermediateDirectories:NO attributes:nil error:&error];
}

For reading and wrting you can use the path.And for more see this Answer

Community
  • 1
  • 1
Buntylm
  • 7,345
  • 1
  • 31
  • 51
  • I understood how to use it. I have changed my question to include few code for explanation. My doubt was in the path returned. – aparna May 15 '13 at 10:51
  • @aparna not too much clear please try my code lines clean code and then test – Buntylm May 15 '13 at 11:24
  • k I will change the code and I will let you know if it works. thanks. – aparna May 15 '13 at 13:10
  • 1
    it's safer to use [... firstObject] instead [... objectAtIndex:0]; example: NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); NSString *cachePath = [paths firstObject]; – Blazej SLEBODA Jun 11 '15 at 09:45
0
-(NSString *) filePath
{

    NSArray *paths = NSSearchPathForDirectoriesInDomains(

                                                         NSDocumentDirectory, NSUserDomainMask, YES);

    NSString *documentsDir = [paths objectAtIndex:0];
    NSLog(@"documentsDir %@",documentsDir);

    return [documentsDir stringByAppendingPathComponent:@"xxxxx.sql"];
}
  • You can get the file path by just calling this function while using sqlite3 *details; int result = sqlite3_open([[self filePath] UTF8String], &details); if(result != SQLITE_OK) – Subhojit Das Sep 08 '15 at 09:01