Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a file exists before creating a new file

Tags:

c++

file

I want to input some contents to a file, but I'd like to check first if a file with the name I wish to create exists. If so, I don't want to create any file, even if the file is empty.

My attempt

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;
}

Is there any way to do what I want?

like image 885
Lion King Avatar asked Jul 23 '13 18:07

Lion King


People also ask

Which function to check if a file already exist before you create it?

exists() function to check if a file exists. To check if a file exists, you pass the file path to the exists() function from the os. path standard library. If the file exists, the exists() function returns True .

How do you check if a file exists in Python and if not create it?

We use the is_file() function, which is part of the Path class from the pathlib module, or exists() function, which is part of the os. path module, in order to check if a file exists or not in Python.

How do I check whether a file exists without exceptions?

How do I check whether a file exists or not, without using the try statement? To check whether a Path object exists independently of whether is it a file or directory, use my_path.exists() . my_path.exists() is not sufficient. my_path.is_file() will tell you if its a file (could be good for reading).


4 Answers

Assuming it is OK that the operation is not atomic, you can do:

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;
}
... 

Note that this doesn't work if you run multiple threads trying to create the same file, and certainly will not prevent a second process from "interfering" with the file creation because you have TOCTUI problems. [We first check if the file exists, and then create it - but someone else could have created it in between the check and the creation - if that's critical, you will need to do something else, which isn't portable].

A further problem is if you have permissions such as the file is not readable (so we can't open it for read) but is writeable, it will overwrite the file.

In MOST cases, neither of these things matter, because all you care about is telling someone that "you already have a file like that" (or something like that) in a "best effort" approach.

like image 180
Mats Petersson Avatar answered Oct 24 '22 02:10

Mats Petersson


you can also use Boost.

 boost::filesystem::exists( filename );

it works for files and folders.

And you will have an implementation close to something ready for C++14 in which filesystem should be part of the STL (see here).

like image 23
alexbuisson Avatar answered Oct 24 '22 03:10

alexbuisson


Try

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

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

or you could use boost functions.

like image 8
CBIII Avatar answered Oct 24 '22 02:10

CBIII


Try this (copied-ish from 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 returns 0 if the file exists and -1 if not.

like image 7
dumbledad Avatar answered Oct 24 '22 04:10

dumbledad