1

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!

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.