如何确保正确打开文件
How to make sure cin opened the file properly?
我有这样的代码:
static std :: ifstream s_inF(argv[j]);
std :: cin.rdbuf(s_inF.rdbuf());
我如何确保它正确打开文件并且没有问题?
我的意思是我想写这样的东西:
static std :: ifstream s_inF(argv[j]);
std :: cin.rdbuf(s_inF.rdbuf());
if(.....)
{
cout << "can not open the file" << endl;
return 0;
}
...
.....
....
cin.close();
有什么建议吗
所有对象都是std::basic_ios
的子类——比如s_inF
和std::cin
,在你的情况下——如果流准备好进行I/O操作,则operator bool
返回true。
这意味着你可以直接测试它们,例如:
static std::ifstream s_inF(argv[j]);
std::cin.rdbuf(s_inF.rdbuf());
if (!s_inF)
{
cout << "can not open the file" << endl;
return 0;
}
// ...
cin.close();
您可以使用is_open
。在这里看到的:
相关文章:
- 如何确保我的"ifstream"文件对象指向的文件内容已更新?
- 如何使用 cmake 来确保 C++14 编译器与实验文件系统 TS 库的链接?
- 尝试使用while循环从文件中读取数据.还必须检查以确保第一个和唯一的值不是-1
- 如何确保文件在生成中仅处理一次
- 如何确保 boost::文件系统::remove 不会尝试删除由其他进程使用的文件
- 如何确保TCP文件传输完成(c++)
- 我如何确保Fopen()相对于可执行的目录打开一个文件,而不是我当前的目录
- 对象文件"Version References"从何而来?
- 我如何确保修改.H文件时,使用Visual Studio 2008将包括其在内的.cc文件自动编译
- 如何确保数据刷新到文件 IO (WriteFile()) 上的 HDD(未缓冲)
- 如何确保我的进程永远不会将另一个进程锁定在文件之外
- 从C++中的文本文件中逐行读取,如何确保输入是双精度的
- 确保多个文件的导出是原子的
- 确保所有对象文件之间的统一 #defines
- C/C++ 预处理器:如何确保包含正确的文件
- 确保文件为(X级)的有效方法?在某个目录下
- 如何写入文件,然后将其读回以验证其内容,确保您获得的是磁盘上的内容而不是缓存
- 如何确保数据结构能适应大文件中的数据
- 我怎样才能确保我可以读取文件,而不是被另一个进程锁定
- 如何确保滴管工具返回一致的值,尽管不同的显示配置文件