C++中IO流的循环读取
ifstream myfile("C:\\Users\\94038\\Desktop\\test_02\\string.txt", ios::in);
string ss;
while (getline(myfile, ss))
{
cout << setw(15) << "分析栈" << setw(15) << "剩余输入串" << setw(15) << "推导式" << endl;
analyExp(ss);
}
myfile.close();
getline函数是读取一行,碰到换行符号\n停止读取一行,然后下一次读取在上次结束位置的下一个开始(也就是下一行)。
txt文件的行数
ifstream myfile("C:\\Users\\94038\\Desktop\\test_02\\stack.txt", ios::in);
char c;
int T0 = 1;//T0是txt文件的行数
while (myfile.get(c))
{
if (c == '\n')
T0++;
}
//cout << T0 << endl;
myfile.close();