积木

No sub title

  C++博客 :: 首页 :: 联系 :: 聚合  :: 管理
  140 Posts :: 1 Stories :: 11 Comments :: 0 Trackbacks

常用链接

留言簿(1)

我参与的团队

搜索

  •  

最新评论

阅读排行榜

评论排行榜

文章转自:http://blogger.org.cn/blog/more.asp?name=hongrui&id=28011
windows平台下使用vc或gcc
函数名: access
功  能: 确定文件的访问权限
用  法: 
int access(const char *filename, int amode); 文件的话还可以检测读写权限,文件夹的话则只能判断是否存在
#include 
"stdafx.h"
#include  
<io.h>
#include  
<stdio.h>
#include  
<stdlib.h>

void main( void )
{
   
/* Check for exist */
   
if( (_access( "C:\\windows"0 )) != -1 )
   {
      printf( 
"windows exists " );
     
      
   }
}
其实判断文件存在fopen就行了。
linux下或者gcc下
#include 
<stdio.h>
#include 
<stdlib.h>

#include 
<dirent.h>
#include 
<unistd.h>
void main( void )
{
 DIR 
*dir = NULL;
     
/* Open the given directory, if you can. */  
    dir 
= opendir( "C:\\windows" );
     
if( dir != NULL ) {
         printf( 
"Error opening " );
         
return ;
     }
}
opendir() 返回的 DIR 指针与 fopen() 返回的 FILE 指针类似,它是一个用于跟踪目录流的操作系统特定的对象。
使用c
++标准库
#include 
"stdafx.h"
#include 
<iostream>
#include 
<fstream>
using namespace std;
#define FILENAME  "C:\\windows"
int main()
{
     fstream file;
     file.open(FILENAME,ios::
in);
     
if(!file)
     {
         cout
<<FILENAME<<"存在";
      }
      
else
      {
          cout
<<FILENAME<<"不存在";
      }
      
return 0;
}
gcc编译去掉#include 
"stdafx.h"即可
使用Windows API PathFileExists

#include 
"stdafx.h"
#include 
<iostream>
#include 
<windows.h>
#include 
<shlwapi.h>
#pragma   comment(lib,
"shlwapi.lib"
using namespace std;
#define FILENAME  "C:\\windows"
int main()
{
      
if (::PathFileExists(FILENAME))
       cout
<<"exist";
    
return 0;
}

注意windows.h 一定要在shlwapi.h前面定义
使用boost的filesystem类库的exists函数:

#include 
<boost/filesystem/operations.hpp>
#include 
<boost/filesystem/path.hpp>
#include 
<boost/filesystem/convenience.hpp>

int GetFilePath(std::string &strFilePath)
{
    
string strPath;
    
int nRes = 0;

    
//指定路径

    strPath 
= "D:\myTest\Test1\Test2";
    
namespace fs = boost::filesystem;

    
//路径的可移植

    fs::path full_path( fs::initial_path() );
    full_path 
= fs::system_complete( fs::path(strPath, fs::native ) );
    
//判断各级子目录是否存在,不存在则需要创建

    
if ( !fs::exists( full_path ) )
    {
        
// 创建多层子目录

        
bool bRet = fs::create_directories(full_path);
        
if (false == bRet)
        {
            
return -1;
        }

    }
    strFilePath 
= full_path.native_directory_string();

    
return 0;
}


posted on 2014-03-23 21:27 Jacc.Kim 阅读(3413) 评论(0)  编辑 收藏 引用

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理