Ondrej
Ondrej

Reputation: 35

open file in command line tool app xcode objective c

I would like to read a simple text file in objective-C (command line tool) this is my code

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{

@autoreleasepool {

    // insert code here...
    NSLog(@"Hello, World!");


    NSBundle *bundle = [NSBundle mainBundle];
    NSString *aPath = [bundle pathForResource:@"data" ofType:@"txt"];




    BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:aPath];

    if (fileExists) {
        NSLog(@"THERE!");
    } else {
        NSLog(@"NOT THERE!");
    }

but I tried full path like /Users/Me/data or added data.txt into xcode but it just won't load the file, what am I missing? thank you!

Upvotes: 1

Views: 1124

Answers (1)

mipadi
mipadi

Reputation: 411370

Command-line tools don't have a bundle associated with them. You'll have to store the file somewhere else (a directory in /Library/Application Support, for example, or perhaps somewhere in /usr/local/share if you're installing the tool to /usr/local) and read it from there.

Upvotes: 2

Related Questions