我该怎么做才能不使用!openFile.eof()
What can I do to not use !openFile.eof()?
我一直在关注 allegro 5 平台游戏的教程,他的文件管理器使用 !openFile.eof(),我听说它不好,我很确定它给了我一个超出范围的矢量下标错误。除了它,我还可以使用什么吗?您还可以检查我的图层类,以防出现矢量下标超出范围错误?我无法弄清楚,我很确定它来自文件管理器,但我无法说出来。
它仅输出地图的第一行。当我把它改成while(std::getline(openFile,line))时,我甚至从来没有到达std::cout
<<content[i][k] <<"k:"<<k <<" content: " <<content[i].size() <<std::endl <<std::endl;当我将其更改为while(std::getline(openFile,line))时,属性和内容每个只能达到8个。
仍然需要帮助=\
文件管理器.CPP
#include "FileManager.h"
FileManager::FileManager()
{
identifierFound = false;
}
FileManager::~FileManager()
{
}
void FileManager::LoadContent(const char *filename, std::vector<std::vector<std::string>> &attributes, std::vector<std::vector<std::string>> &contents)
{
std::ifstream openFile(filename);
std::string line, newLine;
if(openFile.is_open())
{
while(std::getline(openFile, line))
{
std::stringstream str;
if(line.find("Load=") != std::string::npos)
{
type = LoadType::Attributes;
line = line.erase(0, line.find("=") + 1);
tempAttributes.clear();
}
else
{
type = LoadType::Contents;
tempContents.clear();
}
str << line;
while(std::getline(str, newLine, ']'))
{
newLine.erase(std::remove(newLine.begin(), newLine.end(), '['), newLine.end());
std::string erase = " tnr";
newLine.erase(newLine.find_last_not_of(erase) + 1);
if(type == LoadType::Attributes)
tempAttributes.push_back(newLine);
else
tempContents.push_back(newLine);
std::cout << newLine << std::endl;
}
if(type == LoadType::Contents && tempContents.size() > 0)
{
attributes.push_back(tempAttributes);
contents.push_back(tempContents);
}
}
}
else
{
}
}
void FileManager::LoadContent(const char *filename, std::vector<std::vector<std::string>> &attributes, std::vector<std::vector<std::string>> &contents, std::string identifier)
{
std::ifstream openFile(filename);
std::string line, newLine;
if(openFile.is_open())
{
while(!openFile.eof())
{
std::stringstream str;
std::getline(openFile, line);
if(line.find("EndLoad=") != std::string::npos && line.find(identifier) != std::string::npos)
{
identifierFound = false;
break;
}
else if(line.find("Load=") != std::string::npos && line.find(identifier) != std::string::npos)
{
identifierFound = true;
continue;
}
if(identifierFound)
{
if(line.find("Load=") != std::string::npos)
{
type = LoadType::Attributes;
line = line.erase(0, line.find("=") + 1);
tempAttributes.clear();
}
else
{
type = LoadType::Contents;
tempContents.clear();
}
str << line;
while(std::getline(str, newLine, ']'))
{
newLine.erase(std::remove(newLine.begin(), newLine.end(), '['), newLine.end());
std::string erase = " tnr";
newLine.erase(newLine.find_last_not_of(erase) + 1);
if(type == LoadType::Attributes)
tempAttributes.push_back(newLine);
else
tempContents.push_back(newLine);
std::cout << newLine << std::endl;
}
if(type == LoadType::Contents && tempContents.size() > 0)
{
attributes.push_back(tempAttributes);
contents.push_back(tempContents);
}
}
}
}
else
{
}
}
层.CPP
#include "Layer.h"
Layer::Layer(void)
{
}
Layer::~Layer(void)
{
}
std::pair<int, int> Layer::SetTiles(std::string tileString)
{
std::pair<int, int> tile;
tile.first = atoi(tileString.substr(0, tileString.find(',')).c_str());
tile.second = atoi(tileString.substr(tileString.find(',') + 1).c_str());
return tile;
}
void Layer::LoadContent(std::string layerID, std::string mapID)
{
std::string fileName = "Maps/"+mapID+".txt";
fileManager.LoadContent(fileName.c_str(), attributes, contents, layerID);
int indexY = 0;
for(int i = 0; i < attributes.size(); i++)
{
for(int j = 0; j < contents[i].size(); j++)
{
if(attributes[i][j] == "SolidTiles")
{
solidTiles.push_back(SetTiles(contents[i][j]));
std::cout << contents[i][j] << std::endl << std::endl;
}
else if(attributes[i][j] == "TileSheet")
{
std::cout << contents[i][j] << std::endl << std::endl;
tileSheet = al_load_bitmap(contents[i][j].c_str());
}
else if(attributes[i][j] == "StartLayer")
{
for(int k = 0; k < contents[i].size(); k++)
{
std::cout << contents[i][k] << " k: " << k << " contents: " << contents[i].size() << std::endl << std::endl;
if(contents[i][k] != "---")
{
std::cout << contents[i][k] << std::endl << std::endl;
ALLEGRO_BITMAP *tileImage;
Tile::State tempState = Tile::State::Passive;
std::pair<int, int> tile = SetTiles(contents[i][k]);
if(std::find(solidTiles.begin(), solidTiles.end(), tile) != solidTiles.end())
{
tempState = Tile::State::Solid;
}
tileImage = al_create_sub_bitmap(tileSheet, tile.first * 32, tile.second * 32, 32, 32);
std::pair<float, float> position(k * 32, indexY * 32);
Tile tileInstance;
tiles.push_back(tileInstance);
tiles[tiles.size()-1].SetContent(tileImage, tempState, position);
std::cout << tiles.size() << std::endl;
}
}
indexY++;
}
}
}
}
void Layer::UnloadContent()
{
for(int i = 0; i < tiles.size(); i++)
tiles[i].UnloadContent();
al_destroy_bitmap(tileSheet);
}
void Layer::Update()
{
}
void Layer::Draw(ALLEGRO_DISPLAY *display)
{
for(int i = 0; i < tiles.size(); i++)
tiles[i].Draw(display);
}
地图1.txt
Load=[MapProperties]
Load=[TileDimensions]
[32,32]
EndLoad=[MapProperties]
Load=[Layer1]
Load=[SolidTiles]
[2,0]
[1,0]
Load=[NullTile]
[---]
Load=[Motion]
[2,0:Static]
Load=[TileSheet]
[TileSheets/tilesheet1.png]
Load=[StartLayer]
[2,0][---][---][---][---][---][---][---][---][---][---][---][---][---][---][---]
[---][---][---][---][---][---][---][---][---][---][---][---][---][---][---][---]
[---][---][---][---][---][---][---][---][---][---][---][---][---][---][---][---]
[---][---][---][---][---][---][---][---][2,0][2,0][2,0][---][---][---][---][---]
[---][---][---][---][---][---][---][2,0][---][---][---][---][---][---][---][---]
[---][---][---][---][---][---][2,0][---][---][---][---][---][---][---][---][---]
[---][---][---][---][---][---][---][---][---][---][---][---][---][---][---][---]
[---][---][2,0][2,0][2,0][---][---][---][---][---][---][---][---][---][---][---]
[---][---][---][---][---][---][---][---][---][---][---][---][---][---][---][---]
[---][---][---][---][---][---][---][---][---][---][---][---][---][---][---][---]
[1,0][1,0][1,0][1,0][1,0][1,0][1,0][1,0][1,0][1,0][1,0][1,0][1,0][1,0][1,0][1,0]
Load=[EndLayer]
[dummy]
EndLoad=[Layer1]
Load=[PlayerPosition]
[0,0]
首先,真正的问题不是使用file.eof()
;在某些情况下这是你想要的(虽然我想不出它会在哪里一个while
- 合适的情况都是在你拥有之后已检测到输入失败)。 真正的问题是代码使用std::getline
读取的值,而不验证它成功。 由于这在您的代码中仅存在一次,因此只需使用与其他循环中的解决方案相同:while ( std::getline( ... ) )
.
相关文章:
- 如何确定我已使用非编码文件到达 EOF?
- std::ifstream::read 不会读取所有 512 字节,并设置 EOF 和失败位
- 而(!inputfile.eof())只读取第一行?
- EOF有更安全的替代方案吗?它在我的情况下不起作用
- "错误 C0000:语法错误,令牌"<EOF>"处出现意外$end,并且不确定
- 如何在 EOF 之前从文件中读取并将其放入字符串中?
- 如何在从文件中读取整数时使用 file.eof()?
- "eof"可以设置为流吗?
- 吃完 EOF 后重复使用 std::cin
- 文件处理,eof() 定义
- std::cin 在读取 EOF 并清除后不再读取
- 为什么 EOF 在循环扫描期间没有终止?
- cin.clear()是否保留EOF
- 而(getline(fin,str)){}即使在到达eof之后仍在处理数据
- 通过终端在文件中输入时检测EOF(文件结束)时出现问题
- 为什么如果我从键盘输入EOF克里昂不要在运行窗口打印程序的输出?
- 为什么"cin"在EOF之后不再wοrk?
- 在 <char>sizeof(int) == 1 的平台中如何编码 char_traits::eof() ?
- 读取 c++ istream 直到 EOF,同时丢弃内容
- 我该怎么做才能不使用!openFile.eof()