Roger Luo

超越梦想一起飞
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

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

Posted on 2013-05-20 11:54  Roger Luo  阅读(1298)  评论(0编辑  收藏  举报

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