我正在实现文件保存功能Qt使用 C++ 的应用程序。
我正在寻找一种方法来检查所选文件在写入之前是否已存在,以便我可以向用户提示警告。
我正在使用一个std::ofstream
我并不是在寻找Boost解决方案。
这是我最喜欢的隐藏功能之一,我随身携带以供多种用途。
#include <sys/stat.h>
// Function: fileExists
/**
* Check if a file exists
*
* @param[in] filename - the name of the file to check
*
* @return true if the file exists, else false
*/
bool fileExists(const std::string& filename)
{
struct stat buf;
if (stat(filename.c_str(), &buf) != -1)
{
return true;
}
return false;
}
我发现如果您没有立即打算将文件用于 I/O,那么这比尝试打开文件更有品味。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)