C++ 为什么要跳过第一行?
我在尝试将输入文件中的数据导入我的C++ 为什么要跳过第一行?,c++,c++11,C++,C++11,我在尝试将输入文件中的数据导入我的向量时遇到了这个问题。我不知道为什么,但我的代码似乎完全跳过了输入的第一行 以下是输入文件: 1 D 2 C A 3 4 B E 这是我的密码 ifstream myfile1; ifstream myfile2; //take in the input file string line, filename, _block; vector<stack<strin
向量时遇到了这个问题。我不知道为什么,但我的代码似乎完全跳过了输入的第一行
以下是输入文件:
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;
}
ifstream myfile1;
ifstreammyfile2//接收输入文件
字符串行,文件名,_块;
矢量叠加;
堆栈元素;
cout文件名;
// -------------------------------------------------------------------
//打开文件并加载相应向量中的顶点和边
// -------------------------------------------------------------------
myfile1.open(filename.c_str());
if(myfile1.is_open())
{
cout线;
while(getline(myfile1,line))
{
int i;
字符串a;
弦流ss(线);
ss>>i;/“e”
cout在您的while之前(getline(myfile1,line)){
loop,您有myfile1>>行;
,当您第一次调用getline
时,它将读取立即丢弃的数据。也许可以删除myfile1>>行;
行?当您使用调试器检查代码时,您做了哪些观察?您认为myfile>>行如何>是吗?它不是跳过第一行,它只是跳过第一行的第一个数字。