C语⾔⽂件遍历及读写
c语⾔中遍历⽂件或者⽂件夹,系统提供的dirent和DIR结构体中包含了⽂件的很多信息
struct dirent 结构
struct dirent
{
long d_ino; /* inode number 索引节点号 */
off_t d_off; /* offset to this dirent 在⽬录⽂件中的偏移 */
unsigned short d_reclen; /* length of this d_name ⽂件名长 */
unsigned char d_type; /* the type of d_name ⽂件类型 */
char d_name [NAME_MAX+1]; /* file name (null-terminated) ⽂件名,最长255字符 */
}
DIR 结构
struct __dirstream
{
void *__fd; /* `struct hurd_fd' pointer for descriptor. */
char *__data; /* Directory block. */
int __entry_data; /* Entry number `__data' corresponds to. */
char *__ptr; /* Current pointer into the block. */
int __entry_ptr; /* Entry number `__ptr' corresponds to. */
size_t __allocation; /* Space allocated for the block. */
size_t __size; /* Total valid data in the block. */
__libc_lock_define (, __lock) /* Mutex lock for this structure. */
};
typedef struct __dirstream DIR;
struct dirent中的⼏个成员:
d_type:4表⽰为⽬录,8表⽰为⽂件
d_reclen:16表⽰⼦⽬录或⽂件,24表⽰⾮⼦⽬录
经过本⼈亲⾃试验发现:d_reclen:16表⽰⼦⽬录或以.开头的隐藏⽂件,24表⽰普通⽂本⽂件,28为⼆进制⽂件,等等d_name:⽬录或⽂件的名称
具体代码如下,仅供参考:
#include <stdio.h>
#include <dirent.h>
#include <sys/stat.h>
void List(char *path)
{
struct dirent* ent = NULL;
DIR *pDir;
pDir=opendir(path);
while (NULL != (ent=readdir(pDir)))
{
if (ent->d_reclen==24)
{
if (ent->d_type==8)
{
printf("普通⽂件:%s\n", ent->d_name);
}
else
{
printf("⼦⽬录:%s\n",ent->d_name);
List(ent->d_name);
printf("返回%s\n",ent->d_name);
}
}
}
}
int main(int argc, char *argv[])
{
List(argv[1]);
return0;
}
上⾯函数修改后:
void List(char *path)
{
printf("路径为[%s]\n", path);
struct dirent* ent = NULL;
DIR *pDir;
pDir=opendir(path);
/
/d_reclen:16表⽰⼦⽬录或以.开头的隐藏⽂件,24表⽰普通⽂本⽂件,28为⼆进制⽂件,还有其他……
while (NULL != (ent=readdir(pDir)))
{
printf("reclen=%d type=%d\t", ent->d_reclen, ent->d_type);
if (ent->d_reclen==24)
{
//d_type:4表⽰为⽬录,8表⽰为⽂件
if (ent->d_type==8)
{
printf("普通⽂件[%s]\n", ent->d_name);
}
}
else if(ent->d_reclen==16)
{
printf("[.]开头的⼦⽬录或隐藏⽂件[%s]\n",ent->d_name);
}
else
{
printf("其他⽂件[%s]\n", ent->d_name);
}
}
}
下⾯是⽹上来的⼏个⼩例⼦:
#include <stdio.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
void dir_scan(char *path, char *file);
int count = 0;
int main(int argc, char *argv[])
{
struct stat s;
if(argc != 2){
printf("one direction requried\n");
exit(1);
}
if(lstat(argv[1], &s) < 0){
printf("lstat error\n");
exit(2);
}
//判断⼀个路径是否是⽬录
if(!S_ISDIR(s.st_mode)){
printf("%s is not a direction name\n", argv[1]); exit(3);
}
dir_scan("", argv[1]);
printf("total: %d files\n", count);
exit(0);
}
void dir_scan(char *path, char *file)
{
struct stat s;
DIR *dir;
struct dirent *dt;
char dirname[50];
memset(dirname, 0, 50*sizeof(char));
strcpy(dirname, path);
if(lstat(file, &s) < 0){
printf("lstat error\n");
}
if(S_ISDIR(s.st_mode)){
strcpy(dirname+strlen(dirname), file);
strcpy(dirname+strlen(dirname), "/");
if((dir = opendir(file)) == NULL){
printf("opendir %s/%s error\n");
exit(4);
}
if(chdir(file) < 0) {
printf("chdir error\n");
exit(5);
}
while((dt = readdir(dir)) != NULL){
if(dt->d_name[0] == '.'){
continue;
}
dir_scan(dirname, dt->d_name);
}
if(chdir("..") < 0){
printf("chdir error\n");
exit(6);
}
}else{
printf("%s%s\n", dirname, file);
count++;
}
}
linux c 下如何获得⽬录下的⽂件数⽬。
int main(int argc, char **argv)
{
DIR * pdir;
struct dirent * pdirent;
struct stat f_ftime;
int fcnt;/*⽂件数⽬统计*/
pdir=opendir("./");
if(pdir==NULL)
{ return(-1); }
fcnt=0;
for(pdirent=readdir(pdir);pdirent!=NULL;pdirent=readdir(pdir))
{
if(strcmp(pdirent->d_name,".")==0||strcmp(pdirent->d_name,"..")==0) continue;
if(stat(pdirent->d_name,&f_ftime)!=0) return -1 ;
if(S_ISDIR(f_ftime.st_mode)) continue; /*⼦⽬录跳过*/
fcnt++;
printf("⽂件:%s\n",pdirent->d_name);
}
printf("⽂件总数%d\n",fcnt);
closedir(pdir);
return0;
typec转dp}
#include <unistd.h>
#include <stdio.h>
#include <dirent.h>
#include <string.h>
#include <sys/stat.h>
void printdir(char *dir, int depth)
{
DIR *dp;
struct dirent *entry;
struct stat statbuf;
if((dp = opendir(dir)) == NULL) {
fprintf(stderr, "cannot open directory: %s\n ", dir);
return;
}
chdir(dir);
while((entry = readdir(dp)) != NULL) {
lstat(entry-> d_name,&statbuf);
if(S_ISDIR(statbuf.st_mode)) {
/**//* Found a directory, but ignore . and .. */
if(strcmp( ". ",entry-> d_name) == 0 ||
strcmp( ".. ",entry-> d_name) == 0)
continue;
printf( "%*s%s/\n ",depth, "",entry-> d_name);
/**//* Recurse at a new indent level */
printdir(entry-> d_name,depth+4);
}
else printf( "%*s%s\n ",depth, "",entry-> d_name);
}
chdir( ".. ");
closedir(dp);
}
/**//* Now we move onto the main function. */
int main(int argc, char* argv[])
{
char *topdir, pwd[2]= ". ";
if (argc != 2)
topdir=pwd;
else
topdir=argv[1];
printf( "Directory scan of %s\n ",topdir);
printdir(topdir,0);
printf( "done.\n ");
exit(0);
}
注:关于⽂件读取的速度问题
在⽂件⼤⼩相同的前提下:
1.读刚读过的⽂件⽐头次读没读过的⽂件快
2.读转速快的硬盘上的⽂件⽐读转速慢的硬盘上的⽂件快
3.读没有磁盘碎⽚的⽂件⽐读有磁盘碎⽚的⽂件快
4.读⽂件不处理⽐边读边处理快
5.单线程从头到尾⼀次读⽂件⽐多线程分别读⽂件各部分快(⾮固态硬盘上)
6.读固态硬盘上的⽂件⽐读普通硬盘上的⽂件快
顺便提⼀下在c语⾔中读写⽂件:
在C语⾔中写⽂件
//获取⽂件指针
FILE *pFile = fopen("1.txt", //打开⽂件的名称
"w"); // ⽂件打开⽅式如果原来有内容也会销毁
/
/向⽂件写数据
fwrite ("hello", //要输⼊的⽂字
1,//⽂字每⼀项的⼤⼩以为这⾥是字符型的就设置为1 如果是汉字就设置为4
strlog("hello"), //单元个数我们也可以直接写5
pFile //我们刚刚获得到的地址
);
//fclose(pFile); //告诉系统我们⽂件写完了数据更新,但是我们要要重新打开才能在写
fflush(pFile); //数据刷新数据⽴即更新
在C语⾔中读⽂件
FILE *pFile=fopen("1.txt","r"); //获取⽂件的指针
char *pBuf; //定义⽂件指针
fseek(pFile,0,SEEK_END); //把指针移动到⽂件的结尾,获取⽂件长度
int len=ftell(pFile); //获取⽂件长度
pBuf=new char[len+1]; //定义数组长度
rewind(pFile); //把指针移动到⽂件开头因为我们⼀开始把指针移动到结尾,如果不移动回来会出错fread(pBuf,1,len,pFile); //读⽂件
pBuf[len]=0; //把读到的⽂件最后⼀位写为0 要不然系统会⼀直寻到0后才结束
MessageBox(pBuf); //显⽰读到的数据
fclose(pFile); // 关闭⽂件
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论