-1

我不断收到“打开文件失败”。我尝试将“dino.dat”文件放在与运行代码相同的文件夹中。我已经尝试将它放入项目本身。在项目复制的文件下尝试了“构建阶段”选项卡。(????)

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();
    }

我以为我将“dino.dat”复制到代码所在的文件夹中。在 Visual Studio 中,它就是这样工作的。认为 Xcode 可能是相似的。那么,你是在告诉我我应该阅读完整的路径吗:

infile.open ("/Users/me/Desktop/dino.dat")
4

1 回答 1

0

您需要 Xcode 来打开文件吗?或者你编译的程序来打开文件?!两种截然不同的东西。

Xcode 需要打开它
Xcode 可能不想打开一个.dat文件,因为它不知道它是什么。

我的编译程序需要打开它
如果你的编译程序需要打开文件,那么你需要给它适当的文件路径。当 Xcode 编译你的程序时,实际的可执行文件可能在任何地方。它可能在 Xcode 的~/Library/Application Support/..... "dine.dat"是相对文件路径。您的程序认为您的文件与程序位于同一文件夹中。为避免这种情况,请尝试给它一个绝对文件路径。

于 2012-03-11T02:02:38.553 回答