-1

I keep getting "open file failed". I've tried putting the "dino.dat" file in the same folder as the run code. I've tried dropping it into the project itself. Tried "Build Phases" tab under project- copied file there.(????)

void ShowDino()

{

glColor3f(0,0,0);                   //set the drawing color
glLineWidth(3);                 //set the point size

//glBegin(GL_POLYGON);
//glVertex2d(randomPx,randomPy);            //Set the position of the vertex
ifstream infile;
infile.open ("dino.dat");

if(!infile)
{
    cout << "open file failed\n";
    exit(0);
}

N_MAP_POINTS=0;

while(!infile.eof())
{


    infile >> polylines; //read how many polylines
    for(int i = 0; i<polylines; i++)
    {
        infile>>line;
        glBegin(GL_LINE_STRIP);
        for(int j = 0;j<line;j++) //read each lines 
        {
            infile >> x >> y;
            glVertex2d(x, y);
        }
        glEnd();
    }

I thought I copied the "dino.dat" into the folder where the code resides. In Visual Studio, that's how it works. Thought Xcode might be similar. So, are you telling me I should read the full path kind of like:

infile.open ("/Users/me/Desktop/dino.dat")
Henry
  • 1
  • 4
  • I believe you mean `instream infile;` and not `ifstream infile;` –  Mar 11 '12 at 02:06
  • "I copied the dino.dat into the folder where the code resides" This changes nothing as its your _compiled program_ and not your _source code_ that reads your files. And the compiled executable when debugging isn't placed in the project folder. –  Mar 13 '12 at 13:18

1 Answers1

0

You need Xcode to open the file? or your compiled program to open the file?! Two very different things.

Xcode needs to open it
Xcode might not want to open a .dat file because it doesn't know what it is.

My compiled program needs to open it
If your compiled program needs to open the file then you need to give it the appropriate path to the file. When Xcode compiles your program, the actual executable might be anywhere. It could be in Xcode's ~/Library/Application Support/..... "dine.dat" is a relative file path. Your program thinks your file is within the same folder as the program. To avoid that, try giving it an absolute file path.

  • Edited my question some. Tried to add an image, but my reputation isn't big enough apparently. – Henry Mar 11 '12 at 04:53
  • Sorry, I messed up the flow of the question a bit. I'm a newb. It worked! Thanks for the help! – Henry Mar 11 '12 at 05:00