我有这个功能:
void check_open (ifstream& file)
{
if (not file.is_open())
{
cout << "Error." << endl;
exit(1);
}
}
但我只能传递 ifstream 参数,我怎样才能让它也接受 ofstream 参数?
最佳答案
只要流有 is_open()
方法,下面的函数就可以正常工作(fstream
、ifstream
、 ofstream
,以及它们具有不同字符类型的变体)。
template<typename stream_type>
void check_open (const stream_type& file)
{
if (not file.is_open())
{
cout << "Error." << endl;
exit(1);
}
}
关于c++ - 如何将通用文件流作为参数传递?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20172308/