std::ofstream, check if file exists before writing

后端 未结 6 1726
予麋鹿
予麋鹿 2020-12-02 13:18

I am implementing file saving functionality within a Qt application using C++.

I am looking for a way to check to see if the selected file already exists be

相关标签:
6条回答
  • 2020-12-02 13:45

    One of the way would be to do stat() and check on errno.
    A sample code would look look this:

    #include <sys/stat.h>
    using namespace std;
    // some lines of code...
    
    int fileExist(const string &filePath) {
        struct stat statBuff;
        if (stat(filePath.c_str(), &statBuff) < 0) {
            if (errno == ENOENT) return -ENOENT;
        }
        else
            // do stuff with file
    }
    

    This works irrespective of the stream. If you still prefer to check using ofstream just check using is_open().
    Example:

    ofstream fp.open("<path-to-file>", ofstream::out);
    if (!fp.is_open()) 
        return false;
    else 
        // do stuff with file
    

    Hope this helps. Thanks!

    0 讨论(0)
  • 2020-12-02 13:48
    bool fileExists(const char *fileName)
    {
        ifstream infile(fileName);
        return infile.good();
    }
    

    This method is so far the shortest and most portable one. If the usage is not very sophisticated, this is one I would go for. If you also want to prompt a warning, I would do that in the main.

    0 讨论(0)
  • 2020-12-02 13:50

    Try ::stat() (declared in <sys/stat.h>)

    0 讨论(0)
  • 2020-12-02 13:51

    With std::filesystem::exists of C++17:

    #include <filesystem> // C++17
    #include <iostream>
    namespace fs = std::filesystem;
    
    int main()
    {
        fs::path filePath("path/to/my/file.ext");
        std::error_code ec; // For using the noexcept overload.
        if (!fs::exists(filePath, ec) && !ec)
        {
            // Save to file, e.g. with std::ofstream file(filePath);
        }
        else
        {
            if (ec)
            {
                std::cerr << ec.message(); // Replace with your error handling.
            }
            else
            {
                std::cout << "File " << filePath << " does already exist.";
                // Handle overwrite case.
            }
        }
    }
    

    See also std::error_code.

    In case you want to check if the path you are writing to is actually a regular file, use std::filesystem::is_regular_file.

    0 讨论(0)
  • 2020-12-02 14:00
    fstream file;
    file.open("my_file.txt", ios_base::out | ios_base::in);  // will not create file
    if (file.is_open())
    {
        cout << "Warning, file already exists, proceed?";
        if (no)
        { 
            file.close();
            // throw something
        }
    }
    else
    {
        file.clear();
        file.open("my_file.txt", ios_base::out);  // will create if necessary
    }
    
    // do stuff with file
    

    Note that in case of an existing file, this will open it in random-access mode. If you prefer, you can close it and reopen it in append mode or truncate mode.

    0 讨论(0)
  • 2020-12-02 14:10

    This is one of my favorite tuck-away functions I keep on hand for multiple uses.

    #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 find this much more tasteful than trying to open a file if you have no immediate intentions of using it for I/O.

    0 讨论(0)
提交回复
热议问题