linux 下opendir readdir 在windows下的替代函数

转自:http://blog.csdn.net/killerzhou/article/details/2301359

最近在做一个linux下c程序到windows下的移植工程,在移植过程中发现windows下对opendir readdir存在很大问题,经多方查找,终于在http://www.tenouk.com/cpluscodesnippet/viewtopic.php?t=670上找到了解决办法,利用_chdir,_findfist,_findnext函数,很轻松的完成了移植工作。

现把上面链接的程序贴过来,

To show: Using _chdir(), ctime()/ctime_s(), _findfirst(), _findnext(), _findclose() 

Code:
/* The use of the 32-bit _find functions to print a list 
   of all files (and their attributes) in the current directory. */ 
/* Don't forget to put the .h extension to the header files */ 
#include <stdio> 
#include <stdlib> 
#include <io> 
#include <time> 
#include <direct> 
#include <conio> 
#include <ctype> 
#define SIZE 50 

int main() 

   // char   path[50] = "C://WINNT//System32//config"; - Windows 2000 
   char   path[50] = "C://Windows//System32//config"; 
   struct _finddata_t c_file; 
   intptr_t   hFile; 
   char buf[SIZE]; 

   printf("Change to %s/n", path); 
   if(_chdir(path)) 
   { 
      printf("Unable to locate the directory: %s/n", path); 
      exit(1); 
   } 
   else 
      /* Find first in the current directory */ 
      hFile = _findfirst("*.*", &c_file); 

   /* List the files... */ 
   printf("Listing of files in the directory %s/n/n", path); 
   printf("/nRDO HID SYS ARC  FILE                            DATE %20c SIZE/n", ' '); 
   printf("--- --- --- ---  ----                             ---- %20c ----/n", ' '); 
   printf((c_file.attrib & _A_RDONLY) ? " Y  " : " N  "); 
   printf((c_file.attrib & _A_SYSTEM) ? " Y  " : " N  "); 
   printf((c_file.attrib & _A_HIDDEN) ? " Y  " : " N  "); 
   printf((c_file.attrib & _A_ARCH)   ? " Y  " : " N  "); 
   // unsafe version is ctime() 
   ctime_s(buf, SIZE, &(c_file.time_write)); 
   printf(" %-30s %.20s  %9ld/n", c_file.name, buf, c_file.size); 

   /* Find the rest of the files */ 
   while(_findnext(hFile, &c_file) == 0) 
   { 
      printf((c_file.attrib & _A_RDONLY) ? " Y  " : " N  "); 
      printf((c_file.attrib & _A_SYSTEM) ? " Y  " : " N  "); 
      printf((c_file.attrib & _A_HIDDEN) ? " Y  " : " N  "); 
   printf((c_file.attrib & _A_ARCH)   ? " Y  " : " N  "); 
   // unsafe version is ctime() 
   ctime_s(buf, SIZE, &(c_file.time_write)); 
   printf(" %-30s %.20s  %9ld/n", c_file.name, buf, c_file.size); 
   } 
   _findclose(hFile); 

   return 0; 
}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值