0

I am new to developing with XCode 9.2, writing a Cocoa app for MacOS.

I want to maintain a data file (simple CSV text file) within the program.

Lets say the file is called "the_data.dat".

This has been added to the project.

The intention is for this file to ship with the program.

Questions:

What is the path to this file when opening with fopen? How do I ensure it gets shipped - I want it to ship as a viewable text file should we ever need to check its contents.

TenG
  • 3,843
  • 2
  • 25
  • 42
  • 2
    We need more information: is it a command line tool? If yes you can’t bundle anything with it. If it is a cocoa app you can use `Bundle`’s `path/urlForResource(named:)` (or something like that, I’m on mobile sorry) to get the path. – HAS Dec 14 '17 at 11:17
  • @HAS it is a cocoa app. I've updated the question. – TenG Dec 14 '17 at 17:21
  • 1
    What is the path to this file when opening with fopen? -> See above, never did that but it should work ... How do I ensure it gets shipped: As long as it's part of target and copied in the build phase "Copy Bundle Resources" it's shipped :) – HAS Dec 14 '17 at 17:24
  • 1
    Thanks HAS. I got the hint from your answer and used NSBundle *myBundle = [NSBundle mainBundle]; NSString *absPath= [myBundle pathForResource:@"the_file" ofType:@"dat"]; to get the path. Added the file to the project as a text file. The path resolves to ...../Contents/Resource/the_file.dat. – TenG Dec 15 '17 at 20:04
  • 1
    Glad it helped :) I don’t have the time to formulate a full answer but it’s totally fine to answer your own question and mark it as “answered” :) – HAS Dec 16 '17 at 06:12

1 Answers1

0

As per HAS comment:

See comments. Hint from HAS pointed the way:

NSBundle *myBundle = [NSBundle mainBundle];
NSString *absPath= [myBundle pathForResource:@"the_file" ofType:@"dat"];

to get the path. Added the file to the project as a text file.

The path resolves to ...../Contents/Resource/the_file.dat. – TenG yesterday

TenG
  • 3,843
  • 2
  • 25
  • 42