Benches Uncategorized

1

Opening a text file in a bundle with C…

apple Verified

Imported from the Apple Support Forums via the Wayback Machine 🔗

Hi folks! I'm working with bundles for the first time, and I'd like to know how to open a text file inside one with C++. What I have now is this:

ifstream file = new ifstream(fileName.c_str());

if (!file->is_open())

return false;

Where fileName is a std::string. This is the way I'm accustomed to opening files (I'm from a Windows development background -- don't hate me! 🙂 ). Unfortunately, the is_open() test always fails.

I know how to get the path of the bundle. Once I add on my data subdirectory and file name, the fileName variable ends up as this:

/Users/sb/...project location.../Debug/MyProg.app/data/Dev Options.ini

Should the path be different somehow? Thanks for any help!

1 reply

Sign in or join Beachball Help to reply.

  1. 0

    Mark Szymczyk

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Your data folder most likely isn't directly inside the .app bundle. The first directory inside an .app bundle is named Contents. The article at the following URL should help you:

    http://www.meandmark.com/bundlespart1.html

    If you're going to use C++ streams to open files, you'll want to ignore the section on opening the file in Part 3 of the article. You'll want to read up on the Core Foundation CFURL functions. You'll want to use one of the functions that gives you a path to the text file that you can pass to a C++ file stream.