Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Mar 5, 2006, 05:04 PM   #1
XPac27
macrumors newbie
 
Join Date: Mar 2006
C++ File opening

Hi,

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 :

Code:
#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");
}else{ 
	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.
XPac27 is offline   0 Reply With Quote
Old Mar 5, 2006, 05:51 PM   #2
szymczyk
macrumors regular
 
Join Date: Mar 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.
__________________
Mark Szymczyk
Author, Xcode Tools Sensei
http://www.meandmark.com
szymczyk is offline   0 Reply With Quote
Old Mar 5, 2006, 08:23 PM   #3
iSee
macrumors 68040
 
iSee's Avatar
 
Join Date: Oct 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?
iSee is offline   0 Reply With Quote
Old Mar 5, 2006, 09:49 PM   #4
XPac27
Thread Starter
macrumors newbie
 
Join Date: Mar 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/documenta...01119-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 ?
XPac27 is offline   0 Reply With Quote
Old Mar 6, 2006, 01:27 PM   #5
szymczyk
macrumors regular
 
Join Date: Mar 2006
Quote:
Originally Posted by XPac27
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/documenta...01119-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 ?
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.
__________________
Mark Szymczyk
Author, Xcode Tools Sensei
http://www.meandmark.com
szymczyk is offline   0 Reply With Quote
Old Mar 6, 2006, 05:14 PM   #6
WebMongol
macrumors member
 
Join Date: Sep 2004
Location: Bay Area, CA
Quote:
Originally Posted by XPac27
Hi,

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 :

Code:
#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");
}else{ 
	printf ("Doesn't work\n");
}
[skip].
- 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.
WebMongol is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Opening a file in AppleScript Chemical666 Mac Programming 2 May 7, 2014 06:47 PM
Multiple users opening the same file?! Coxy Mac OS X Server, Xserve, and Networking 0 Oct 21, 2013 11:38 PM
Wine help, opening exe file Gunwind Windows, Linux & Others on the Mac 3 Aug 3, 2013 04:57 PM
opening .mpg file on iPad 2 lopakalolo iPad Tips, Help and Troubleshooting 1 Feb 5, 2013 12:27 PM
Opening an RAR file! Timshan0876 OS X 4 Jul 5, 2012 08:59 AM

Forum Jump

All times are GMT -5. The time now is 04:29 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC