linux文件操作
2011-07-12 14:52
197 查看
#include <stdio.h>
#include <stdlib.h>
#include <sys/dir.h>
#include <sys/stat.h>
#include <string.h>
/* 判断是否为目录 */
int IS_DIR(const char* path)
{
struct stat st;
lstat(path, &st);
return S_ISDIR(st.st_mode);
}
/* 获得当前路径 */
char* _getcwd(char* _DstBuf,int _SizeInBytes)
{
if( _DstBuf == NULL )
return NULL;
int count = readlink("/proc/self/exe",_DstBuf,_SizeInBytes);
if( count < 0 || count >= _SizeInBytes )
{
printf("Get Cur Path Error!\n");
return NULL;
}
_DstBuf[count] = '\0';
return _DstBuf;
}
/* 遍历文件夹de递归函数 */
void List_Files_Core(const char *path, int recursive)
{
DIR *pdir;
struct dirent *pdirent;
char temp[256];
pdir = opendir(path);
if(pdir)
{
while(pdirent = readdir(pdir))
{
/* 跳过"."和".." */
if(strcmp(pdirent->d_name, ".") == 0
|| strcmp(pdirent->d_name, "..") == 0)
continue;
sprintf(temp, "%s/%s", path, pdirent->d_name);
printf("%s\n", temp);
/* 当temp为目录并且recursive为1的时候递归处理子目录 */
if(IS_DIR(temp) && recursive)
{
List_Files_Core(temp, recursive);
}
}
}
else
{
printf("opendir error:%s\n", path);
}
closedir(pdir);
}
/* 遍历文件夹的驱动函数 */
void List_Files(const char *path, int recursive)
{
int len;
char temp[256];
/* 去掉末尾的'/' */
len = strlen(path);
strcpy(temp, path);
if(temp[len - 1] == '/') temp[len -1] = '\0';
if(IS_DIR(temp))
{
/* 处理目录 */
List_Files_Core(temp, recursive);
}
else /* 输出文件 */
{
printf("%s\n", path);
}
}
#include <stdlib.h>
#include <sys/dir.h>
#include <sys/stat.h>
#include <string.h>
/* 判断是否为目录 */
int IS_DIR(const char* path)
{
struct stat st;
lstat(path, &st);
return S_ISDIR(st.st_mode);
}
/* 获得当前路径 */
char* _getcwd(char* _DstBuf,int _SizeInBytes)
{
if( _DstBuf == NULL )
return NULL;
int count = readlink("/proc/self/exe",_DstBuf,_SizeInBytes);
if( count < 0 || count >= _SizeInBytes )
{
printf("Get Cur Path Error!\n");
return NULL;
}
_DstBuf[count] = '\0';
return _DstBuf;
}
/* 遍历文件夹de递归函数 */
void List_Files_Core(const char *path, int recursive)
{
DIR *pdir;
struct dirent *pdirent;
char temp[256];
pdir = opendir(path);
if(pdir)
{
while(pdirent = readdir(pdir))
{
/* 跳过"."和".." */
if(strcmp(pdirent->d_name, ".") == 0
|| strcmp(pdirent->d_name, "..") == 0)
continue;
sprintf(temp, "%s/%s", path, pdirent->d_name);
printf("%s\n", temp);
/* 当temp为目录并且recursive为1的时候递归处理子目录 */
if(IS_DIR(temp) && recursive)
{
List_Files_Core(temp, recursive);
}
}
}
else
{
printf("opendir error:%s\n", path);
}
closedir(pdir);
}
/* 遍历文件夹的驱动函数 */
void List_Files(const char *path, int recursive)
{
int len;
char temp[256];
/* 去掉末尾的'/' */
len = strlen(path);
strcpy(temp, path);
if(temp[len - 1] == '/') temp[len -1] = '\0';
if(IS_DIR(temp))
{
/* 处理目录 */
List_Files_Core(temp, recursive);
}
else /* 输出文件 */
{
printf("%s\n", path);
}
}
相关文章推荐
- Linux文件操作
- linux的一些常用操作(随时更新)命令导入sql文件
- Linux文件打开操作
- 再说linux中的rm mv 遍历执行多个文件的操作: find + xagrs
- linux中的操作文件
- Linux下启动关闭多个tomcat清除logs日志文件删除War包等操作,版2.0
- linux下操作乱码文件
- linux -文件整体操作
- Linux实用代码(1)--文件系统操作
- linux文件操作总结(上)
- Linux 目录结构及文件基本操作
- 小何讲Linux: 底层文件I/O操作
- Linux文件基本属性和操作
- (转载)Linux入门:操作目录和文件的命令
- linux 下利用vi操作文件相关
- Linux 命令:文件目录操作与实例
- Linux文件权限操作
- linux文件操作常用命令
- [原]linux文件操作
- Linux中shell文件操作大全