在C++中打开std :: ifstream文件的错误(一半作品)
问题描述:
抱歉打扰你!现在我在我的游戏中有一个“bug”,如果有人能指出我在做错什么,这将会非常有帮助。发生什么事是当我通过Xcode运行我的代码时,TileMap加载文件的好处。在C++中打开std :: ifstream文件的错误(一半作品)
但是,当我得到生成的可执行文件,将其复制并粘贴到不同的位置,然后将所有需要的数据文件粘贴到文件夹(.png文件,.txt文件)中时,游戏可以正常工作,除了事实该地图不会加载。
我不确定这个bug是否是由于我在IDE中运行它时单独更改文件位置所致。
图片:(对不起,我没有足够的信誉使这里的链接)在Xcode
游戏工作运行: 游戏不是从Xcode中运行:
这是代码的一部分TileMap.cpp与从文本文件中的数据加载到一个数组交易(只是为了告诉你它的工作原理):
int t_map[20][15] {};
TileMap::TileMap(std::string fileName, std::string picFile)
{
std::ifstream inFS;
inFS.open(fileName.c_str());
if(!(inFS.is_open()))
{
std::cout << "Could not open file.\n";
return;
}
//controlls row
int tt = 0;
while(!inFS.eof())
{
char buff[100];
//move to buffer thing
inFS.getline(buff, 100);
const char * token[20] = {};
int j = 0;
token[0] = strtok(buff, ",");
if(token[0])
{
for(j=1;j<COLMAX;j++)
{
token[j] = strtok(0, ",");
if(!token[j])
break;
}
}
for(int i=0;i<COLMAX;i++)
{
t_map[i][tt] = atoi(token[i]);
}
tt++;
}
inFS.close();
//ends the loading the file and starts the image stuff
TextureManager::Instance()->load(picFile, "tileMap", Game::Instance()->getRenderer());
}
文本文件的内容我传:
2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2
2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2
4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,3,3,3,3
4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,3,3,3,3
4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,3,3,3,3
4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,3,3,3,3
4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,3,3,3,3
4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,3,3,3,3
4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,3,3,3,3
4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,3,3,3,3
4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,3,3,3,3
4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,3,3,3,3
4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,3,3,3,3
2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2
2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2
非常感谢您的阅读和帮助。
答
我找到了自己问题的答案。我需要获取基本路径,然后让系统从那里搜索文件。
可能的重复[为什么iostream :: eof里面的循环条件被认为是错误的?](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-错误) – Wintermute 2015-04-02 08:09:34
你如何建立'fileName'和'picFile'参数?你有没有检查路径实际上是正确的?换句话说,它从文件中读取失败还是根本找不到文件? – Ionut 2015-04-02 08:34:37
我假设文件路径是正确的,因为它在xCode中运行时工作。 – KojinKuro 2015-04-02 08:46:39