Moh*_*bil 8 c++ file-io fstream getline
检查这个程序
ifstream filein("Hey.txt");
filein.getline(line,99);
cout<<line<<endl;
filein.getline(line,99);
cout<<line<<endl;
filein.close();
Run Code Online (Sandbox Code Playgroud)
Hey.txt文件中有很多字符.超过1000
但我的问题是为什么我第二次尝试打印线.它不打印?
Ker*_* SB 39
因此,从流中读取行的惯用方法是:
{
std::ifstream filein("Hey.txt");
for (std::string line; std::getline(filein, line); )
{
std::cout << line << std::endl;
}
}
Run Code Online (Sandbox Code Playgroud)
注意:
不close().当习惯使用时,C++会为您处理资源管理.
使用free std::getline,而不是stream成员函数.
| 归档时间: |
|
| 查看次数: |
80502 次 |
| 最近记录: |