我想從文件讀取填充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