views:

78

answers:

1

Hi, I'm new to mac and Cocoa so I'm sorry if this is a stupid question..

I need to read all the lines I wrote on a file I saved on my desktop. The file format is .txt; I tried with stringWithContentsOfFile but the program freezes. I tried with GDB and I noticed that, while the path is correct, the string which is supposed to contain the data returns nil.

Am I missing something important?

+3  A: 

You need to use a path not just the filename

NSString *string;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
if ([paths count] != 0) {
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *newFile = [documentsDirectory stringByAppendingPathComponent:@"filename.txt"];
    NSFileManager *fm = [NSFileManager defaultManager];
    if ([fm fileExistsAtPath:newFile]) {
        string = [NSString stringWithContentsOfFile:newFile];
    }
}

You will need to replace the documents directory for the bundle directory if you are reading from there.

EDIT

I jumped the gun. It seems that this is NOT an iPhone question. None-the-less, you will need to pass in the full path, not just the filename

coneybeare