检查真实文件后假文件“存在”?

时间:2017-12-27 07:08:30

标签: c++ windows filepath

我一直在Linux上编写C ++编程,但最近搬到了Windows 10计算机上。

我设法用w64-mingw设置CodeBlocks。

我一直在尝试将程序从linux移动到Windows,我遇到了文件名问题。例如,我有代码来检查文件或目录是否存在,以及创建目录。但是我得到了奇怪的结果,如果文件检查恢复为真,那么所有后续文件检查都会返回true。我有示例代码,其中test.txt和testdir是最初不存在但由程序创建的文件和目录。 fail.txt和faildir永远不存在,但我的程序声称它们在创建test.txt和testdir之后就存在了。我已经看到几个关于检查Windows上是否存在文件的问题,但我从未遇到过这样的行为,而且我不确定发生了什么。当调用GetFileAttributes()时,Windows是否无法重新初始化某些内容?或者我错过了一些非常基本的东西?

的main.cpp

#include <iostream>
#include <fstream>
#include "../include/FileChecker.h"

int main(){
    FileChecker fc = FileChecker();
    std::cout << "Test Start" << std::endl;

    #ifdef _WIN32
    std::cout << "OS is windows" << std::endl;
    #endif // _WIN32
    std::cout << std::endl;
    std::cout << "Nothing should exist" << std::endl;

    if(fc.file_exists("test.txt")){
        std::cout << "test.txt exists." << std::endl;
    }else{
        std::cout << "test.txt does not exist." << std::endl;
    }
    if(fc.file_exists("fail.txt")){
        std::cout << "fail.txt exists." << std::endl;
    }else{
        std::cout << "fail.txt does not exist." << std::endl;
    }

    if(fc.directory_exists("testdir")){
        std::cout << "Directory testdir exists." << std::endl;
    }else{
        std::cout << "Directory testdir does not exist." << std::endl;
    }
    if(fc.directory_exists("faildir")){
        std::cout << "Directory faildir exists." << std::endl;
    }else{
        std::cout << "Directory faildir does not exist." << std::endl;
    }
    std::cout << std::endl;
    std::cout << "Creating test.txt" << std::endl;
    std::ofstream test("test.txt");
    test << "HELLO" << std::endl;
    test.close();

    std::cout << "Only test.txt should exist" << std::endl;
    if(fc.file_exists("test.txt")){
        std::cout << "test.txt exists." << std::endl;
    }else{
        std::cout << "test.txt does not exist." << std::endl;
    }
    if(fc.file_exists("fail.txt")){
        std::cout << "fail.txt exists." << std::endl;
    }else{
        std::cout << "fail.txt does not exist." << std::endl;
    }
    if(fc.directory_exists("testdir")){
        std::cout << "Directory testdir exists." << std::endl;
    }else{
        std::cout << "Directory testdir does not exist." << std::endl;
    }
    if(fc.directory_exists("faildir")){
        std::cout << "Directory faildir exists." << std::endl;
    }else{
        std::cout << "Directory faildir does not exist." << std::endl;
    }
    std::cout << std::endl;
    std::cout << "Creating directory testdir" << std::endl;
    if(fc.create_directory("testdir")){
        std::cout << "Creation Success" << std::endl;
    }else{
        std::cout << "Creation Failed" << std::endl;
    }

    std::cout << "Only testdir should exist" << std::endl;
        if(fc.directory_exists("testdir")){
        std::cout << "Directory testdir exists." << std::endl;
    }else{
        std::cout << "Directory testdir does not exist." << std::endl;
    }
    if(fc.directory_exists("faildir")){
        std::cout << "Directory faildir exists." << std::endl;
    }else{
        std::cout << "Directory faildir does not exist." << std::endl;
    }

    return 0;
}

FileChecker.h

    #ifndef FILECHECKER_H
    #define FILECHECKER_H


    #ifdef _WIN32
    #include <windows.h>
    #include <tchar.h>
    #include <stdio.h>
    #include <direct.h>
    #endif // _WIN32

    #include <string>


    class FileChecker
    {
        public:

            FileChecker();
            virtual ~FileChecker();

            bool file_exists(std::string filename);
            bool directory_exists(std::string dirname);
            bool create_file(std::string filename);
            bool create_directory(std::string dirname);

        protected:

        private:
    };

    #endif // FILECHECKER_H

FileChecker.cpp

#include "../include/FileChecker.h"

FileChecker::FileChecker(){
    //ctor

}


FileChecker::~FileChecker(){
    //dtor

}



#ifdef _WIN32
bool FileChecker::file_exists(std::string filename){
    static LPCTSTR szPath = TEXT(filename.c_str());
    DWORD dwAttrib = GetFileAttributes(szPath);
    return ((dwAttrib != INVALID_FILE_ATTRIBUTES) && !(dwAttrib & FILE_ATTRIBUTE_DIRECTORY));
}
#endif // _WIN32


#ifdef _WIN32
bool FileChecker::directory_exists(std::string dirname){
    static LPCTSTR szPath = TEXT(dirname.c_str());
    DWORD dwAttrib = GetFileAttributes(szPath);
    return ((dwAttrib != INVALID_FILE_ATTRIBUTES) && (dwAttrib & FILE_ATTRIBUTE_DIRECTORY));
}
#endif // _WIN32


#ifdef _WIN32
bool FileChecker::create_directory(std::string dirname){
    static LPCTSTR szPath = TEXT(dirname.c_str());
    return(CreateDirectory(szPath, NULL));
}
#endif // _WIN32

输出

enter image description here

2 个答案:

答案 0 :(得分:3)

您应该删除函数中的所有static个关键字。

bool FileChecker::file_exists(std::string filename){
static LPCTSTR szPath = TEXT(filename.c_str());  // <--- [*]
DWORD dwAttrib = GetFileAttributes(szPath);

第一次调用file_exists函数时,会创建szPath变量并初始化,指向filename的字符数组。当您第二次调用file_exists时,szPath的值仍然相同,并指向无效数据(保留指向文件名对象数据的指针,该对象在第一次调用file_exists后被删除)

您应该阅读函数中的静态变量。

答案 1 :(得分:1)

您的代码在这里:

bool FileChecker::file_exists(std::string filename){    
    static LPCTSTR szPath = TEXT(filename.c_str());
    DWORD dwAttrib = GetFileAttributes(szPath);
    return ((dwAttrib != INVALID_FILE_ATTRIBUTES) && !(dwAttrib & FILE_ATTRIBUTE_DIRECTORY));
}

TEXT只是强制转换,它不执行任何类型的转换。请改为:

bool FileChecker::file_exists(std::string filename)
{
    DWORD dwAttrib = GetFileAttributesA(filename.c_str());
    return ((dwAttrib != INVALID_FILE_ATTRIBUTES) && !(dwAttrib & FILE_ATTRIBUTE_DIRECTORY));
}