C++ File opening

Discussion in 'Mac Programming' started by XPac27, Mar 5, 2006.

  1. XPac27 macrumors newbie

    Mar 5, 2006

    I'm making an OpenGL application in C++ on Mac OS X 10.4.2 using a MakeFile.
    I made an "Object" class that will be able to read a .OBJ file to construct a shape. The problem is that whatever function I use to open the file, the file doesn't get opened :

    #include <iostream>
    #include <fstream>
    using namespace std;
    ... some code
    FILE * pFile = fopen ("cube_s10.obj", "rt");
    if (pFile==NULL) printf ("File not opened\n");
    ifstream file ("cube_s10.obj");
    if (file.is_open()){
    	printf ("Works !\n");
    	printf ("Doesn't work\n");
    The code get compiled without any errors but when I run the Application it outputs both "File not opened" and "Doesn't work".

    The .OBJ file is in the same directory than the C++ file. I tryed to put it in all the other directories of my project, to write the file path differently ... nothing works. I test the same code in XCode and it works. I'm really disappointed.

    I looked for a solution on all the net and didn't find any working one ... maybe you can help me ?
    Thank you in advance.
  2. szymczyk macrumors regular

    Mar 5, 2006
    Your code isn't working because the program doesn't know where to look for the OBJ file. In Xcode you can set the working directory for the program by selecting the executable file from the Groups and Files list and clicking the Info button.

    Most Mac OS X applications use application bundles and store the files they need inside the bundle. The Core Foundation framework contains functions to read files from application bundles. Using application bundles is safer than relying on working directories.
  3. iSee macrumors 68040


    Oct 25, 2004
    The way your code is written, cube_s10.obj needs to be in your current working directory. So put it in folder A. In terminal, cd to folder A and execute your app. To make it easy, you could put the app in the same folder. Then you could type "./myapp"

    Does that work?
  4. XPac27 thread starter macrumors newbie

    Mar 5, 2006
    Thank you so much for your help !
    This works like a charm when I lunch my app from the console and write the file path regarding to the app location. :)

    So if I want to make an application that I can start from Finder I have to create a bundle (actually it's the first time I make an application for Mac) ... It looks like a Bundle is something used by Finder, where every thing about your application's environment is referenced: http://developer.apple.com/document...my.html#//apple_ref/doc/uid/20001119-BAJFIBAJ

    Is it possible to build an application using a make file and then, when it's done, to create a Bundle for it ?
    Is a bundle a package that contains the App or just a script that describes your application's folders ?
  5. szymczyk macrumors regular

    Mar 5, 2006
    A bundle is a group of directories that looks like a single file in the Finder. There are several types of bundles in Mac OS X, one of which is an application bundle. An application bundle contains the executable file and any supporting files the application needs.

    Xcode takes care of a lot of the work in creating application bundles, but you can create application bundles from a makefile. This forum post from iDevApps contains an example of creating a Carbon application bundle from a makefile.
  6. WebMongol macrumors member

    Sep 19, 2004
    Bay Area, CA
    - Use absolute pathname for your file - for example, /Users/XPac27/<project_path>/cube_s10.obj
    - for debugging you can pass file name as command line argument
    $ ls -l /Users/XPac27/<project_path>/cube_s10.obj # make sure that file does exist
    $ ./myApp /Users/XPac27/<project_path>/cube_s10.obj

    - in you app replace "cube_s10.obj" with argv[1] and make sure that your main function has following arguments:
    main(int argc, char** argv) { ... }
    You do not need bundles to open a file.

Share This Page