获取目录中的文件信息

程序功能:

获取当前工作目录名
更改当前工作目录
获取当前工作目录下的文件信息


代码:

#include   < stdio.h >
#include  
< direct.h >   // _getcwd(), _chdir()
#include  < stdlib.h >   // _MAX_PATH, system()
#include   < io.h >   // _finddata_t, _findfirst(), _findnext(), _findclose()

void  main(  void  )
{
   
char  buffer[_MAX_PATH];

   
// 获取当前工作目录
    if ( _getcwd( buffer, _MAX_PATH )  ==  NULL )
      perror( 
" _getcwd error "  );
   
else
      printf( 
" %s/n " , buffer );

   
// 更改当前工作目录 - 相对路径方式
    if ( _chdir(  " ./temp "  ) )
       printf( 
" Unable to locate the directory you specified /n "  );
   
else
   
{
       _getcwd( buffer, _MAX_PATH ); 
// 重新获取当前工作目录
       printf(  " The CWD is: %s/n " , buffer );  // 输出当前工作目录
       system(  " type hello.c " );  // system用于执行命令行指令
   }


   
// 更改当前工作目录 - 绝对路径方式
    if ( _chdir(  " F://temp "  ) )  // 双反斜杠处理转义字符'/'
       printf(  " Unable to locate the directory you specified /n "  );
   
else
   
{
       _getcwd( buffer, _MAX_PATH );
       printf( 
" /n/nThe CWD is: %s/n " , buffer );
       system( 
" dir *.* " );
   }


   
// 查找当前目录中符合要求的文件, 并输出文件的相关信息
     long  hFile;
    _finddata_t fileinfo;
    
if  ((hFile = _findfirst( " *.c " , & fileinfo))  !=   - 1L )
    
{
        
do
        
{            
            
if  ( ! (fileinfo.attrib  &  _A_SUBDIR))  // 检查是不是目录, 如果不是,则进行处理
             {
                printf(
" %s, %ul bytes/n " , fileinfo.name, fileinfo.size);
            }

        }
  while  (_findnext(hFile, & fileinfo)  ==   0 );
        _findclose(hFile);
    }

}



截图:


                           图一、默认的工作目录(也即当前工程所在目录)


                      图二、更改后的工作目录(注:先要确保新的工作目录存在)


                                                        图三、输出结果

分类: C/C++
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值