0

Possible Duplicate:
Read Text file Programmatically using Objective-C

I am using the following lines of Code ::

- (void)sync:(NSString *)savedName{

NSLog(@"saved name is this :: %@", savedName);

NSString *savedDir = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
NSString *saveFilePath = [savedDir stringByAppendingPathComponent:savedName];
saveFilePath = [saveFilePath stringByAppendingPathComponent:@"edit.txt"];

NSString *saveString = [NSString stringWithContentsOfFile:saveFilePath encoding:NSUTF8StringEncoding error:nil]; 

NSLog(@"saveString is :: %@ savedName : %@ savefilepath : %@", saveString, savedName, saveFilePath);
}

The contents of edit.txt is :

= WWWW =
[[Category:ssss]]

What I am trying to do is to read from the file edit.txt and store its contents in a string. However, on NSLogging, I get null for saveString.

I got the following output at gdb ::

splitView[838:f803] saveString is :: (null) savedName : xxxx savefilepath : /Users/xxxx/Library/Application Support/iPhone Simulator/5.1/Applications/D4B3A4CF-E7D0-4D25-B3D3A170A329/Documents/xxxx/edit.t‌​xt`

Can someone help me to sort out the error ?? I am unable to figure it out. Thanks and Regards.

Community
  • 1
  • 1
kamalbhai
  • 510
  • 2
  • 10
  • 23

4 Answers4

1

I think that your path to the TXT file is wrong. Check this out the way you are building the saveFilePath, maybe:

NSString *savedDir = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
NSString *saveFilePath = [savedDir stringByAppendingPathComponent:savedName];
saveFilePath = [saveFilePath stringByAppendingPathComponent:@"edit.txt"];
Jonathan Naguin
  • 14,526
  • 6
  • 46
  • 75
1

If ou try :

NSString *savedDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *saveFilePath = [savedDir stringByAppendingPathComponent:savedName];
//Why do you do that ? saveFilePath = [savedDir stringByAppendingPathComponent:@"edit.txt"];

NSString *saveString = [NSString stringWithContentsOfFile:saveFilePath encoding:NSUTF8StringEncoding error:nil]; 

NSLog(@"saveString is :: %@ savedName : %@ savefilepath : %@", saveString, savedName, saveFilePath);
Pierre
  • 10,593
  • 5
  • 50
  • 80
  • I have more than 1 .txt files in the folder of `savedName`. So, I tried using the specific name for my .txt file. – kamalbhai Jul 03 '12 at 08:10
  • I tried your code too .. still it comes out to be null. However, can you explain me as to what the first line of yours does ?? – kamalbhai Jul 03 '12 at 08:15
  • When I read your code I suspect you to look in the Document directory of your application. Perhaps I'm wrong but if not my code is more common to look on this directory. Please print all the path (with your file name inside) and show us. – Pierre Jul 03 '12 at 08:17
  • I got the following output :: `splitView[838:f803] saveString is :: (null) savedName : xxxx savefilepath : /Users/xxxx/Library/Application Support/iPhone Simulator/5.1/Applications/D4B3A4CF-E7D0-4D25-B3D3A170A329/Documents/xxxx/edit.txt` – kamalbhai Jul 03 '12 at 08:26
  • Now fill the error parameter with an NSError variable – Pierre Jul 03 '12 at 08:45
  • did not get you .. how can I do that ?? – kamalbhai Jul 03 '12 at 08:46
1

Check whether your document contain edit.txt file.

Edit:

- (void)sync:(NSString *)savedName{

    NSString *filePath = [self applicationDocumentsDirectory];
    filePath = [filePath stringByAppendingPathComponent:@"license.txt"];
    NSString *saveString = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil]; 

    NSLog(@"saveString is :: %@ savedName : %@ savefilepath : %@", saveString, savedName, filePath);
}

Path of license.txt is: /Users/nuzhat.zari/Library/Application Support/iPhone Simulator/5.0/Applications/9BFCE2B5-C976-4EF6-91DF-DA4ABCD04788/Documents.

Make sure your file path is also similar to this.

Nuzhat Zari
  • 3,398
  • 2
  • 24
  • 36
1

Ok then what I used to read data from .txt file is as below

NSString *path;
path = [[NSBundle mainBundle] pathForResource: YOUR_TEXT_FILE_NAME ofType: @"txt"];

Now a function to read the file which returns NSString as return object. My call to this function is as below

NSString *data = [self readFile: path]];

Now the function

-(NSString *)readFile:(NSString *)fileName

{
    NSLog(@"readFile");

    NSString *appFile = fileName;    
    NSFileManager *fileManager=[NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:appFile])        
    {
        NSError *error= NULL;
        NSString *resultData = [NSString stringWithContentsOfFile: appFile encoding: NSUTF8StringEncoding error: &error]; 
        if (error == NULL)            
            return resultData;
    }
    return NULL;
}

Hope this will help you

Happy Coding :)

The iOSDev
  • 5,237
  • 7
  • 41
  • 78
  • how should I define my path here ?? In my code .. I think I have defined my path correctly. – kamalbhai Jul 03 '12 at 09:11
  • just use this to read out from the file not to save any file – The iOSDev Jul 03 '12 at 09:14
  • i.e. just for `NSString *saveString = [self readFile: path]];` where `path` is same as given above in my answer just put proper name instead of `YOUR_TEXT_FILE_NAME` – The iOSDev Jul 03 '12 at 09:16
  • please tell me how to define the path .. I am getting all the more confused. – kamalbhai Jul 03 '12 at 09:17
  • ok `path = [[NSBundle mainBundle] pathForResource:edit ofType: @"txt"];` if your file name is `edit.txt` just put `savedName` instead of `edit` because you use `savedName` to save files. Right? – The iOSDev Jul 03 '12 at 09:20
  • thanks for the help .. I figured it out .. so long .. Ihad been specifying the wrong path. shitt .. I am really sorry for consuming your time :( It was my bad all the way – kamalbhai Jul 03 '12 at 09:27
  • It's my pleaser to help you and also my help is useful to you. YWC – The iOSDev Jul 03 '12 at 09:29