C++用getline(fin,str)读取文本文件的ifstream对象fin每一行,哪种写法较好,结果正确?
C++用getline(fin,str)读取文本文件的ifstream对象fin每一行,哪种写法较好,结果正确?
ifstream fin("a.txt");
string str;
1、
while(!fin.eof())
{
getline(fin,str);
cout<<str<<endl;
}
2、
getline(fin,str);
while(!fin.eof())
{
cout<<str<<endl;
getline(fin,str);
}
3、
while(getline(fin,str))
{
cout<<str<<endl;
}
或者有没有别的写法?
ifstream fin("a.txt");
string str;
1、
while(!fin.eof())
{
getline(fin,str);
cout<<str<<endl;
}
2、
getline(fin,str);
while(!fin.eof())
{
cout<<str<<endl;
getline(fin,str);
}
3、
while(getline(fin,str))
{
cout<<str<<endl;
}
或者有没有别的写法?
...全文
相关推荐
使用ifstream和getline读取文件内容[c++]
str[LINE_LENGTH]; while (fin.getline(str, LINE_LENGTH)) { std::cout << "Read from file: " << str <<
读取字符串,按行,逐字
" << endl; ifstream fin("data.DAT"); const int LINE_LENGTH = 1000; char str[LINE_LENGTH]; while (fin.getline
C++中用substr()函数消除前后空格的解决方法详解
int main() { string newstring = ""; vector<string> str; ifstream fin("a.txt"); string line; // 读取文件的每一行
c++读取csv文件数据
"C++编程,读取CSV文件数据并存储到数组中"在C++编程中,处理CSV(Comma Separated Values)文件是常见的任务,尤其在数据分析、数据导入导出等场景下。CSV文件是一种简
一些语言的按行读取文件的代码实现小结
以下是C++的示例代码:```cpp#include <iostream>#include <fstream>#include <string>int main() { std::ifstream fin


