<分区>
从文件中读取时,我们有两种方式
方式一:
ifstream fin("data.txt");
const int LINE_LENGTH = 100;
char str[LINE_LENGTH];
while( fin.getline(str,LINE_LENGTH) )
{
cout << "Read from file: " << str << endl;
}
方式二:
ifstream fin("data.txt");
string s;
while( getline(fin,s) )
{
cout << "Read from file: " << s << endl;
}
哪个更好?就个人而言,我更喜欢 way2,因为我不需要指定最大长度,你有什么意见?