Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading from text file - Objective C

I am trying to familiarize myself with objective C, and my current goal is to read a list of items in a text file and store them in a NSString array.

Currently this is what I have:

NSString *filepath = [[NSBundle mainBundle] pathForResource:@"myList" ofType:@"txt"];
NSData* data = [NSData dataWithContentsOfFile:filepath];
NSString* string = [[NSString alloc] initWithBytes:[data bytes]
                                             length:[data length]
                                           encoding:NSUTF8StringEncoding];

NSString* delimiter = @"\n";
listArray = [string componentsSeparatedByString:delimiter];

I am not sure if this matters, but myList.txt is in my Supporting Files.

At the moment, I only have one item in my list. However I am unable to store even that 1 item into my listArray.

I am sure it is something silly that I am missing, I am just new to Objective C.

EDIT: I apologize for not mentioning this earlier. I AM NOT receiving any sort of error. My array is just null.

like image 551
Johnrad Avatar asked Apr 25 '13 19:04

Johnrad


People also ask

How do I read a file in Objective C?

Firstly, the contents of a file may be read and stored in an NSData object through the use of the contentsAtPath method: NSFileManager *filemgr; NSData *databuffer; filemgr = [NSFileManager defaultManager]; databuffer = [filemgr contentsAtPath: @"/tmp/myfile. txt" ];

How do I open an Objective C source file?

FILE *libFile = fopen("/Users/pineapple/Desktop/finalproj/test242. txt","r"); char wah[200]; fgets(wah, 200, libFile); printf("%s test\n", wah);


2 Answers

I'm going to suggest a little simplification which might solve your problem since I can't say what your problem is. From the information I'm not sure if you are getting the proper file contents when reading it in or not.

NSString *filepath = [[NSBundle mainBundle] pathForResource:@"myList" ofType:@"txt"];
NSError *error;
NSString *fileContents = [NSString stringWithContentsOfFile:filepath encoding:NSUTF8StringEncoding error:&error];

if (error)
    NSLog(@"Error reading file: %@", error.localizedDescription);

// maybe for debugging...
NSLog(@"contents: %@", fileContents);

NSArray *listArray = [fileContents componentsSeparatedByString:@"\n"];
NSLog(@"items = %d", [listArray count]);  
like image 105
DBD Avatar answered Sep 22 '22 11:09

DBD


If the content of the file is just like:

[{"Title":"20","Cost":"20","Desc":""},{"Title":"10","Cost":"10.00","Desc":""},{"Title":"5","Cost":"5.00","Desc":""}]

try this

-(id)readFromDocumentDBFolderPath:(NSString *)fileName
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *appFile = [documentsDirectory stringByAppendingPathComponent:fileName];
    NSFileManager *fileManager=[NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:appFile])
    {
        NSError *error= NULL;
        NSData* data = [NSData dataWithContentsOfFile:appFile];
        id resultData = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
        if (error == NULL)
        {
            return resultData;
        }
    }
    return NULL;
}
like image 39
ChenYilong Avatar answered Sep 18 '22 11:09

ChenYilong