我想从文件读取填充2D矢量(矢量向量)。C++ while(getline(file,str))does not work
void readFromFile()
{
string str;
ifstream fileToOpen("test.txt");
if (fileToOpen.is_open())
{
resetBoard();
height=count(istreambuf_iterator<char>(fileToOpen),istreambuf_iterator<char>(), '\n');
length=height*2-1;
cout<<board.size()<<endl;
while(getline(fileToOpen,str))
{
cout<<"inside while\n";
stringstream ss(str);
int i;
vector<short unsigned> line;
while (ss>>i)
{
line.push_back(i);
}
board.push_back(line);
}
fileToOpen.close();
//board.shrink_to_fit();
}
else
cout<<"Unable to open file!\n";
print();
}
可以忽略大多数不涉及实际的文件打开和读取/写入向量代码。问题是,第一,而循环似乎没有执行
while(getline(fileToOpen,str))
{
cout<<"inside while\n";
COUT语句根本不会触发,我只是无法弄清楚这究竟是为什么,我一直在使用全新的矢量尝试,而不是使用一个我在课堂上,不同的印刷,方法等,直到我想通了,它从来没有真正到达该点
我的文本文件
0000000000000001000000000000000
0000000000000011100000000000000
0000000000000110010000000000000
0000000000001101111000000000000
0000000000011001000100000000000
0000000000110111101110000000000
0000000001100100001001000000000
0000000011011110011111100000000
0000000110010001110000010000000
0000001101111011001000111000000
0000011001000010111101100100000
0000110111100110100001011110000
0001100100011100110011010001000
0011011110110011101110011011100
0110010000101110001001110010010
1101111001101001011111001111111
调用'的std ::计数()'后使用流迭代器,流中的位置是在年底。使用“长度”或“高度”是什么意思? – 0x499602D2 2014-10-29 20:56:47
@ 0x499602D2这些变量是全局类变量,在方法 – 2014-10-29 21:21:46