����Linux�Ż�
Linux����

Linux��Windows�ı���Ŀ¼�������ļ��ķ����Ա�

����ʱ��:2017-02-20 14:54:16��Դ:linux��վ����:Harley_Quinn
�������߶�ȡ�����ļ��ķ������Dz��õ����ķ�ʽ�������ú���A�ķ���ֵ�ж�Ŀ¼���Ƿ����ļ���Ȼ�󷵻�ֵ�Ϸ�����ѭ�����ú���Bֱ������B�ķ���ֵ���Ϸ�Ϊֹ������ú���C�ͷ���Դ��
 
1����Ŀ¼
#include <sys/types.h>
#include <dirent.h>
DIR *opendir(const char *name);
�ȿ�Linux�ģ����ص���DIR*����˳���ʱ����NULL(0)�������ﲻ�ù���DIR�ṹ���嶨�壬ֻ��Ҫ֪���Ƕ������в�����ע�⣺DIR���DZ����ļ���Ϣ�Ľṹ��
��Windows�ķ����ܶ࣬����MFC��CFileFind�ࡢWINAPI��WIN32_FIND_DATA��C���п��_finddata_t������ѡȡ���һ��
intptr_t _findfirst( 
const char *filespec, 
struct _finddata_t *fileinfo  
); 
����������intptr_t������������ʾ����ָ�루��ַ��֮���������ͣ������ָ������p1,p2����intptr_t������p1-p2�ķ���ֵ��������ָ����32λϵͳ����4���ֽڣ�����64λϵͳ����8���ֽڣ�ͨ��#ifdef����ʵ�ֿ�ƽ̨�����͡�
���ﷵ��ֵ��ʵ��һ����ʶ��ǰĿ¼�������ļ���HANDLE������ʵ��ָ�����ͣ����Գ���ʱ����-1������0(NULL)��
�ٿ������������1��������filespec��������filename�����������ô�����Ϊ��������Linux��opendirһ���򵥵ؽ���Ŀ¼�������ǽ���һ���ض���ʽ������C:\*.*�ʹ�������C�������������ļ�����C:\*.txt���������C��������txt�ļ�����2��������struct _finddata_t��ʵ�ʴ洢�ļ���Ϣ�Ľṹ��
 
2�������ļ�
ÿ���ļ�����һ������Ľṹ�������������ԣ�����ֻ���ļ�����Ϊʾ�����������Բ�����̽�����嶨����Բ����ĵ���
#include <dirent.h>
struct dirent *readdir(DIR *dirp);
Linux�µķ�����Ȼ�ܼ򵥣�ͨ����һ���õ���DIR*��Ϊ��������������ɹ��򷵻�ָ��ǰ�ļ���dirent*��struct dirent�������ļ���Ϣ�Ľṹ
struct dirent {
ino_t          d_ino;       /* Inode number */
off_t          d_off;       /* Not an offset; see below */
unsigned short d_reclen;    /* Length of this record */
unsigned char  d_type;      /* Type of file; not supported by all filesystem types */
char           d_name[256]; /* Null-terminated filename */
};
�����ȡʧ���򷵻ؿ�ָ��NULL(0)
�ٿ�Windows�µķ���
int _findnext( 
intptr_t handle, 
struct _finddata_t *fileinfo  
); 
���ﷵ��ֵ��int�������dz���ʱ����-1����Cû���쳣�������ƣ����Dz������صĴ�������ƣ����ǵ������ó�ѧ�߸е�����������⣺����0�����Ǵ�����ȷ���Dz���ȷ�أ��Դ�������ԣ�0����������ȷ������ָ�����0�����ʧ�ܣ�Ҳ���ǿ�ָ��NULL��
��1���������Ҳ�ǵ�1�������ķ���ֵ����2���������Ҳ�������ļ��Ľṹ���ָ�롣struct _finddata_t�������ļ���Ϣ�Ľṹ����Ҳ�Ǹ��磨Windows��ƽ̨�Ķ��壬��32λϵͳΪ��
struct _finddata32_t
{
unsigned    attrib;
__time32_t  time_create;    // -1 for FAT file systems
__time32_t  time_access;    // -1 for FAT file systems
__time32_t  time_write;
_fsize_t    size;
char        name[260];
};
 
3���ر�Ŀ¼
#include <sys/types.h>
#include <dirent.h>
int closedir(DIR *dirp);
Linux�µģ���������ǵ�1�������ķ���ֵ�������ﷵ��ֵ������ָ�룬���Ǵ����룬��˷���ֵΪ0ʱ�����ر�directory stream�ɹ���Ϊ-1����ʧ��
int _findclose(  
intptr_t handle  
); 
Windows�µ�Ҳһ������������ǵ�1�������ķ���ֵ������0�����ر�handle�ɹ���Ϊ-1����ʧ�ܡ�
 
���ֱ����Linux��Windows�ϱ���Ŀ¼�������ļ���ʾ�����루Ϊ�˼򻯺��Դ�������
###############################
#include <stdio.h>
#include <dirent.h> 
int main(int argc, char** argv)
{
struct dirent *direntp;
DIR *dirp = opendir("/");
if (dirp != NULL) {
while ((direntp = readdir(dirp)) != NULL)
printf("%s\n", direntp->d_name);
}
closedir(dirp);
return 0;
}
###############################
// Windows(C++)
#include <stdio.h>
#include <stdlib.h>
#include <io.h>  // windows��CRT��
#include <string>
int main()
{
_finddata_t fd;
intptr_t handle;
std::string dir_name = "C:\\";
if ((handle = _findfirst((dir_name + "*.*").c_str(), &fd)) != -1) {
while (_findnext(handle, &fd) != -1)
printf("%s\n", fd.name);
}
_findclose(handle);
return 0;
}
###############################
����Windows�»�Ҫ��dir_name����һ���ַ�������ֱ����std::string�ˣ���char����Ȼ��strcpy̫�鷳��
�Աȿ��Է��֣�Windows�ǰ��ļ��ṹ��ָ����Ϊ�����������Linux������Ϊ���ز�����Linux�µ�����������Ϊ��Ȼ�����Ҽ�ʹ�õ���C��񣬴���Ҳ�dz����׶���
����Ҫע�⣬Linux��������ʵ�����Ƕ�̬�����˿ռ䣬��Ҫ�ֶ�free(direntp)���ͷ��ڴ棬��ȻAPUE�����ʾ�����벢û����һ����
 
�������ø��µ�ַ��//m.ajphoenix.com/linux/28564.html