C++ 文件系统操作 File System Operation

Create Directory

Using Windows api, include <windows.h>

    if (0 == ::CreateDirectoryA(_root.c_str(), nullptr))

    {

        DWORD errcode = ::GetLastError();

        if (errcode == ERROR_ALREADY_EXISTS)

        {

            return 0;

        }

        throw ISATGrabberException(CUtility::GetSysErrInfomation(errcode), errcode, __FILE__, __FUNCTION__, __LINE__);

    }

Using C api in windows, include <direct.h>

    int ret = _mkdir(_root.c_str());

    if (ret != 0 && errno == ENOENT)

    {

        printf("%s, %d", strerror(errno), errno);

        return ret;

    }

Check Direcotry exist

Using Windows api

 

 

 

Using C api in windows, include <sys/stat.h> and <sys/types.h>

        struct _stat buf;

        int ret = _stat(_root.c_str(), &buf); 



        CPPUNIT_ASSERT(0 == ret);

        CPPUNIT_ASSERT((buf.st_mode & _S_IFDIR) > 0);

Using boost library

ASSERT(boost::filesystem::status(_root).type() == boost::filesystem::directory_file);

 

Remove Directory

Using Windows API

 

Using C api in windows, include <direct.h>

_rmdir(dir.c_str());

Using boost library

你可能感兴趣的:(System)