错误地读取文件
incorrectly reading in file
如果我给我的程序txt文件:
BB
MB
150 570 2
240 570 3
360 570 0
FB
E
T
它不正确地读取它,而是将其读取为
BB
150 0 0
240 570 2
360 570 3
0 570 0
MB
FB
E
T
下面是我用来阅读这篇文章的简化版本:
string one,two,three,four;
ifstream file;
filename+=".txt";//filename is a string
file.open(filename.c_str());
while (file >> one >> two>>three&&one!="MB")
{
//do stuff with it here
}
等等。有人能解释一下为什么2和3最初被设为0吗?
完整版代码:
读:void load(string filename)
{
string one,two,three,four;
ifstream file;
filename+=".txt";
file.open(filename.c_str());
//blocks
//backblock list
while (file >> one >> two>>three&&one!="MB")
{
backBlockList.push_back(
Block(atoi(two.c_str()),atoi(three.c_str()),atoi(one.c_str())));
}
while (file >> one >> two>>three&&one!="FB")
{
midBlockList.push_back(
Block(atoi(two.c_str()),atoi(three.c_str()),atoi(one.c_str())));
}
while (file >> one >> two>>three&&one!="E")
{
foreBlockList.push_back(
Block(atoi(two.c_str()),atoi(three.c_str()),atoi(one.c_str())));
}
while (file >> one &&one!="T")
{
enemyList.push_back(Enemy(atoi(one.c_str())));
//loads waypoints
while (file >> one>>two )
{
enemyList.at(enemyList.size()-1).addWaypoint(
atoi(one.c_str()),atoi(two.c_str()));
}
while(file>>one>>two>>three>>four)
{
textBlockList.push_back(
TextBlock(atoi(one.c_str()),atoi(two.c_str())));
textBlockList.at(
textBlockList.size()-1).setText(three);
textBlockList.at(
textBlockList.size()-1).setRange(atoi(four.c_str()));
}
}
}
写:void printOut(string filename )
{
cout<<"printing "<<endl;
ofstream myfile;
filename+=".txt";
myfile.open (filename.c_str());
myfile << "BBn";
//prints out blocks
cout<<"printing backblocks";
unsigned int i = 0;
for( i = 0; i<backBlockList.size(); i++)
{
backBlockList.at(i).print(myfile);
}
cout<<" printed "<<i<<endl;
cout<<"printing midblocks";
myfile << "MBn";
for( i = 0; i<midBlockList.size(); i++)
{
midBlockList.at(i).print(myfile);
}
cout<<" printed "<<i<<endl;
cout<<"printing foreblocks";
myfile << "FBn";
for( i = 0; i<foreBlockList.size(); i++)
{
foreBlockList.at(i).print(myfile);
}
cout<<" printed "<<i<<endl;
cout<<"printing enemies "<<endl;
myfile<<"En";
for( i =0; i<enemyList.size(); i++)
{
enemyList.at(i).print(myfile);
}
cout<<"printing text";
myfile<<"Tn";
for( i =0; i<textBlockList.size(); i++)
{
if(textBlockList.at(i).complete())
textBlockList.at(i).print(myfile);
}
cout<<" printed "<<i<<endl;
cout<<"printing other"<<endl;
//Additional stuff goes here EX BACKGROUND
myfile.close();
cout<<"printing done";
}
块写:
void Block::print(ofstream & file)
{
file << x;
file << " ";
file<< y;
file<< " ";
file<< Type;
file<< " n";
}
TextBlock写:
void TextBlock::print(ofstream & file)
{
file<< x;
file<<" ";
file<< y;
file<<" ";
file<< text;
file<<" ";
file<<range;
file<<" n";
}
敌人写:
void Enemy::print(ofstream & file)
{
file<<type;
for(unsigned int i =0; i<X.size()-1; i++)
{
file<<" ";
file<< X.at(i);
file<<" ";
file<< Y.at(i);
}
file<<"n";
}
我希望它以如下方式读取文件:
BB MB 150
570 2 240
570 3 360
570 0 FB
E T
因为它总是一次读取三个字符串。如果您希望始终读取三个字符串,您可能希望在MB和BB指示器中填充虚拟0
以读取(例如MB 0 0
)。
可能有助于认识到
cin >> a >> b >> c;
在处理换行符方面与cin >> a; cin >> b; cin >> c;
没有什么不同。
你得到的数字三元组的原因:
150 0 0
240 570 2
360 570 3
0 570 0
只从第一个循环读取输入:
while (file >> one >> two>>three&&one!="MB")
像:Loop | one | two | three | atoi | atoi | atoi
| | | | one | two | three
----------------------------------------------
1 | BB | MB | 150 | 0 | 0 | 150
2 | 570 | 2 | 240 | 570 | 2 | 240
3 | 570 | 3 | 360 | 570 | 3 | 360
4 | 570 | 0 | FB | 570 | 0 | 0
5 | breaks the loop because three can't be read
表中的最后三列是观察到的数字三元组
file >> one >> two>>three&&one!="FB"
从文件中读取三个字符串而不是一个。
相关文章:
- 正在将csv文件读取为双精度矢量
- 将从格式化文本文件读取的文本数据存储到链表
- C++ 读取文件读取文件不正确
- 使用对象文件读取三角形数据网格
- 如何减慢从 BVH 文件读取的 opengl 动画?
- 从文件读取,并写入函数C++中的数组
- 从对象C++中的文件读取内容时出现分段错误
- 从文件读取并通过矢量输出到控制台
- 尝试将字符串从文件读取到无符号字符向量中
- 如何将文本文件读取到二维数组中并以 c++ 打印
- 简化文件读取的函数
- 如何将逗号分隔的文件读取为 2D int 数组?
- 在 c++ 中将数据从文件读取到队列中
- 'std::wstring_convert'尽可能多地转换(从 UTF8 文件读取块)
- C++,从文件读取到结构,然后读取到向量(结构被推入向量太多次,而不仅仅是一次)
- 文件读取C++包括空格键
- 从文件读取时未将值推回矢量
- 从文件 C++ 读取时的无限循环
- 从同一文件读取和写入?
- 从.txt文件读取到C++中的双精度向量