为什么它跳过第一行
Why does it skip the first line?
我遇到了这个问题,试图从输入文件中获取数据到我的vector<stack<string>>
。我不知道为什么,但看起来我的代码完全跳过了第一行输入下面是输入文件:
1 D
2 C A
3
4 B E
我的代码
ifstream myfile1;
ifstream myfile2; //take in the input file
string line, filename, _block;
vector<stack<string>> _stack;
stack <string> _elements;
cout << "Please enter name of beginning state" << endl;
cin >> filename;
// -------------------------------------------------------------------
// OPEN FILE AND LOAD VERTICES AND EDGES IN APPROPRIATE VECTOR
// -------------------------------------------------------------------
myfile1.open(filename.c_str());
if (myfile1.is_open())
{
cout << "File found!" << endl;
myfile1 >> line;
while (getline(myfile1, line))
{
int i;
string a;
stringstream ss ( line );
ss >> i; // "e"
cout<<"Test stupid: "<< i <<endl;
while( ss >> a )
{
_elements.push(a);
cout <<"Test dump: "<< a <<endl;
}
_stack.push_back(_elements);
//cout <<"Test: "<<_elements.top()<<endl;
num_of_stacks = _stack.size();
num_of_elements = _elements.size();
}
//cout <<"Test: "<<_elements.top()<<endl;
while(!_elements.empty())
{
string w = _elements.top();
cout <<"Test1: "<< w <<endl;
_elements.pop();
}
cout <<"Test2: "<<num_of_stacks<<endl;
cout <<"Test3: "<<num_of_elements<<endl;
}
在您的while (getline(myfile1, line)) {
循环之前,您有myfile1 >> line;
,它将读取数据,当您第一次调用getline
时立即丢弃。也许删除myfile1 >> line;
行?
相关文章:
- 我正在使用嵌套的while循环来解析具有多行的文本文件,但由于某种原因,它只通过第一行,我不知道为什么
- 为什么如果我添加这一行,我的程序会不断询问值
- 为什么这个.c文件只有一行"../xx/xx.c"?
- 为什么这一行不输出我的文本文件?
- 为什么调试器总是跳过此程序中的一行?
- 为什么我不能在同一行中定义两个相同类型的类的成员指针
- 为什么只有一行告诉我操作员=不可行?但是其他人很好
- 为什么我在输出的开头多了一行
- 为什么我的程序在这一行崩溃
- 为什么"#pragma omp 关键"部分不能在同一行上具有左大括号?
- 为什么仅在CSV文件中打印第一行
- 为什么 getline 没有移动到下一行
- 为什么_GLIBCXX_DEBUG必须设置在第一行?
- 为什么这个函数从不超出第一行
- 无法弄清楚为什么只从文件中读取一行矩阵以及为什么我无法将 2D 数组传递给函数
- C++为什么不可能将这两个操作合并在一行中?
- 为什么此代码只能识别文本文件中的第一行?输入任何大于 1 的数字都会返回"Atomic Number Not Found"
- 为什么输入文件的最后一行运行两次?
- C和MATLAB:为什么MATLAB中的一行成为MATLAB编码器生成的C 代码中的许多行
- 为什么在同一行上有 2 个函数调用时跳过函数调用