jshill103
jshill103

Reputation: 320

read/write from a .txt file iOS 7 objc

I have this code:

  NSString *studentList = _textIn.text;
NSString *path = [[self applicationDocumentsDirectory].path
                  stringByAppendingPathComponent:@"ClassOne.txt"];
[studentList writeToFile:path atomically:YES
               encoding:NSUTF8StringEncoding error:nil];
NSLog(@"%@", studentList);
NSLog(@"students 1 saved");

I try to write to a text file on the phone system. When I try to read from that file with:

NSError *error;
NSString *strFileContent = [NSString stringWithContentsOfFile:[[NSBundle mainBundle]
                                                               pathForResource: @"classOne" ofType: @"txt"] encoding:NSUTF8StringEncoding error:&error];
_textIn.text = strFileContent;

The textfield comes up blank. The NSLog prints what was in the text field but when I try to load it up later the textfield is empty. how do I get the textfield to display what is in the file.

Upvotes: 0

Views: 353

Answers (1)

Dani-san
Dani-san

Reputation: 296

The writing part is alright. You are trying to read the file from your application main bundle instead of your original file path.

Upvotes: 2

Related Questions