如何在创建新文件之前检查文件是否存在

时间:2013-07-23 18:25:00

标签: c++ file

我想将一些内容输入到文件中,但我想首先检查是否存在名称我想创建的文件。如果是这样,即使文件为空,我也不想创建任何文件。

我的尝试

bool CreateFile(char name[], char content[]){
     std::ofstream file(name);
     if(file){
         std::cout << "This account already exists" << std::endl;
        return false;
     }
     file << content;
     file.close();
     return true;
}

有什么办法可以做我想要的吗?

9 个答案:

答案 0 :(得分:26)

假设操作不是原子的,你可以这样做:

if (std::ifstream(name))
{
     std::cout << "File already exists" << std::endl;
     return false;
}
std::ofstream file(name);
if (!file)
{
     std::cout << "File could not be created" << std::endl;
     return false;
}
... 

请注意,如果您运行多个线程尝试创建相同的文件,则这不起作用,当然也不会阻止第二个进程“干扰”文件创建,因为您遇到TOCTUI个问题。 [我们首先检查文件是否存在,然后创建它 - 但是其他人可以在检查和创建之间创建它 - 如果这很关键,你将需要做一些其他的,这是不可移植的]。

另一个问题是,如果您具有诸如文件不可读的权限(因此我们无法打开它以进行读取)但是可写,则它将覆盖该文件。

在大多数情况下,这些事情都不重要,因为你所关心的只是在“尽力而为”的方法中告诉别人“你已经拥有这样的文件”(或类似的东西)。

答案 1 :(得分:13)

你也可以使用Boost。

 boost::filesystem::exists( filename );

适用于文件和文件夹。

你将有一个接近C ++ 14准备的实现,其中文件系统应该是STL的一部分(参见here)。

答案 2 :(得分:7)

尝试

ifstream my_file("test.txt");
if (my_file)
{
 // do stuff
}

来自:How to check if a file exists and is readable in C++?

或者您可以使用增强功能。

答案 3 :(得分:7)

试试这个(来自Erik Garrison的复制品:https://stackoverflow.com/a/3071528/575530

#include <sys/stat.h>

bool FileExists(char* filename) 
{
    struct stat fileInfo;
    return stat(filename, &fileInfo) == 0;
}
如果文件存在,

stat会返回0,否则会返回-1

答案 4 :(得分:6)

从C ++ 17开始,有:

if (std::filesystem::exists(pathname)) {
   ...

答案 5 :(得分:2)

看了一下,我发现的唯一一件事是使用open系统调用。它是我发现的唯一一个允许你创建文件的函数,如果文件已经存在则会失败

#include <fcntl.h>
#include <errno.h>

int f=open(filename, O_WRONLY | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
if (f < 0) {
  /* file exists or otherwise uncreatable
     you might want to check errno*/
}else {
  /* File is open to writing */
}

请注意,您必须在创建文件时提供权限。

这也消除了可能存在的任何竞争条件

答案 6 :(得分:0)

我刚看到这个测试:

bool getFileExists(const TCHAR *file)
{ 
  return (GetFileAttributes(file) != 0xFFFFFFFF);
}

答案 7 :(得分:0)

C ++ 17,跨平台:使用std::filesystem::existsstd::filesystem::is_regular_file

#include <filesystem> // C++17
#include <fstream>
#include <iostream>
namespace fs = std::filesystem;

bool CreateFile(const fs::path& filePath, const std::string& content)
{
    try
    {
        if (fs::exists(filePath))
        {
            std::cout << filePath << " already exists.";
            return false;
        }
        if (!fs::is_regular_file(filePath))
        {
            std::cout << filePath << " is not a regular file.";
            return false;
        }
    }
    catch (std::exception& e)
    {
        std::cerr << __func__ << ": An error occurred: " << e.what();
        return false;
    }
    std::ofstream file(filePath);
    file << content;
    return true;
}
int main()
{
    if (CreateFile("path/to/the/file.ext", "Content of the file"))
    {
        // Your business logic.
    }
}

答案 8 :(得分:-1)

最简单的方法是使用> RSA_WITH_3DES_EDE_CBC_SHA > RSA_WITH_AES_128_CBC_SHA > RSA_WITH_AES_256_CBC_SHA