15
程序员文章站
2022-08-28 11:37:16
ios类定义了这四个常量badbit, eofbit, failbit, goodbit这四个标志常量就是取对应标志位的掩码,也即输入的四种异常情况!以上四个常量对应的取值为:ios::badbit 001 输入(输出)流出现致命错误,不可挽回ios::eofbit 010 已经到达文件尾ios::failbit 100 输入(输出)流出现非致命错误,可挽回ios::goodbit 000 流状态完全正常, 各异常标志位都为0就一般说来,badbit是一些系...
ios类定义了这四个常量badbit, eofbit, failbit, goodbit
这四个标志常量就是取对应标志位的掩码,也即输入的四种异常情况!
以上四个常量对应的取值为:
ios::badbit 001 输入(输出)流出现致命错误,不可挽回
ios::eofbit 010 已经到达文件尾
ios::failbit 100 输入(输出)流出现非致命错误,可挽回
ios::goodbit 000 流状态完全正常, 各异常标志位都为0
就一般说来,badbit是一些系统底层或者硬件出错,比如文件系统错误,磁盘错误,网络错误等等。failbit就是其他软件错误,如试图从不能解析为整数的字符串里想要读一个整数等,顺便说下eof也会造成failbit被置位。
//以上转载于百度知道
首先建立 fish.text文件, 存入数据:18 19 18.5 13.5 14 16 19.5 20 18 12 18.5 17.5
const int SIZE = 20;
char filename[SIZE];
ifstream inFile;
cout << "enter the filename: ";
cin.getline(filename, SIZE);
inFile.open(filename);//输入fish.text
if (!inFile.is_open())
{
cout << "can not open the file! " << endl;
exit(EXIT_FAILURE);
}
double value;
double sum = 0.0;
int count = 0;
inFile >> value;
while (inFile.good())//while input good and not at EOF
{
++count;
sum += value;
cout << value << endl;
inFile >> value;
}
if (inFile.eof())
cout << "End of file reached.\n";
else if (inFile.fail())
cout << "input terminated by data mismatch.\n";
else
cout << "input terminated for unknown reason.\n";
if (count == 0)
cout << "No data processed.\n";
else
{
cout << "items read:" << count << endl;
cout << "sum: " << sum << endl;
cout << "Average: " << sum / count << endl;
}
inFile.close();
本文地址:https://blog.csdn.net/qq_44886707/article/details/107204801
下一篇: android自定义注解的运用
推荐阅读