c语言opendir
C修改文件名:使用rename函数。
rename函数:功能描述:改变文件的名称或者位置,如果目标已存在,将被自动覆盖。用法:#include<stdio.h>intrename(constchar*oldpath,constchar*newpath);参数:
oldpath:旧文件名。newpath:新文件名或者新位置。
具体可以分以下2种情况:
1、修改单个文件
直接使用rename即可。
2、批量修改文件(如:按一定规则修改某目录下所有文件)
需要使用opendir遍历目录,然后修改该目录下文件。下面提供一个简单的例子。
voidModFilesName(constchar*pcszPath)
{
charszPathFile[1024]={0};//路径+文件名
DIR*dir_p;
structdirent*direntp;
structstatentryInfo;
//文件目录不存在,则创建
if(stat(pcszPath,&entryInfo)<0)
{
printf("Autocreatefolder:%s ",pcszPath);
mkdir(pcszPath,0755);
}
if((dir_p=opendir(pcszPath))==NULL)
{
return;
}
while((direntp=readdir(dir_p))!=NULL)
{
//组合完整路径
sprintf(szPathFile,"%s/%s",pcszPath,direntp->d_name);
//判断文件是否是目录
if(lstat(szPathFile,&entryInfo)==0)
{
if(S_ISDIR(entryInfo.st_mode))
{
continue;//忽略目录
}
rename(szPathFile,你要修改成的文件名);
}
}//while(...
closedir(dir_p);
}
推荐一片文章:http://blog.chinaunix.net/uid-7525568-id-251530.html
希望能帮助到你,你的好评是我前进的动力!谢谢!
B. c语言读取文件的路径怎么设定
//获取指定目录下的所有文件列表 author:wangchangshaui jlu
char** getFileNameArray(const char *path, int* fileCount)
{
int count = 0;
char **fileNameList = NULL;
struct dirent* ent = NULL;
DIR *pDir;
char dir[512];
struct stat statbuf;
//打开目录
if ((pDir = opendir(path)) == NULL)
{
myLog("Cannot open directory:%s\n", path);
return NULL;
}
//读取目录
while ((ent = readdir(pDir)) != NULL)
{ //统计当前文件夹下有多少文件(不包括文件夹)
//得到读取文件的绝对路径名
snprintf(dir, 512, "%s/%s", path, ent->d_name);
//得到文件信息
lstat(dir, &statbuf);
//判断是目录还是文件
if (!S_ISDIR(statbuf.st_mode))
{
count++;
}
} //while
//关闭目录
closedir(pDir);
// myLog("共%d个文件\n", count);
//开辟字符指针数组,用于下一步的开辟容纳文件名字符串的空间
if ((fileNameList = (char**) myMalloc(sizeof(char*) * count)) == NULL)
{
myLog("Malloc heap failed!\n");
return NULL;
}
//打开目录
if ((pDir = opendir(path)) == NULL)
{
myLog("Cannot open directory:%s\n", path);
return NULL;
}
//读取目录
int i;
for (i = 0; (ent = readdir(pDir)) != NULL && i < count;)
{
if (strlen(ent->d_name) <= 0)
{
continue;
}
//得到读取文件的绝对路径名
snprintf(dir, 512, "%s/%s", path, ent->d_name);
//得到文件信息
lstat(dir, &statbuf);
//判断是目录还是文件
if (!S_ISDIR(statbuf.st_mode))
{
if ((fileNameList[i] = (char*) myMalloc(strlen(ent->d_name) + 1))
== NULL)
{
myLog("Malloc heap failed!\n");
return NULL;
}
memset(fileNameList[i], 0, strlen(ent->d_name) + 1);
strcpy(fileNameList[i], ent->d_name);
myLog("第%d个文件:%s\n", i, ent->d_name);
i++;
}
} //for
//关闭目录
closedir(pDir);
*fileCount = count;
return fileNameList
C. C语言readdir和opendir
readdir和opendir都是目录流操作,挨个读取或者打开目中的文件知道不为空或者如果为空,也就是第一个没读完就继续循环,第二个如果打开完了就执行if条件句里边的
D. 请教C语言怎么获取本路径下指定后缀名的文件并打开
opendir打开目录循环获取目录下每个文件节点判断后缀为txt对于所有符合的,对比创建时间,保存最早的。
E. 如何用c语言列出目录树
给你一个参考:
相关函数:opendir
表头文件:#include <ftw.h>
定义函数:int ftw(const char *dir, int (*fn) (const *file, const struct stat *sb, int flag), int depth)
函数说明:ftw() 会从参数dir指定的目录开始,往下一层层地递归式遍历子目录。ftw()会传三个参数给fn(), 第一个参数*file指向当时所在的目录路径,第二个参数是*sb, 为stat结构指针,第三个参数为旗标,有下面几种可能值
FTW_F 一般文件
FTW_D 目录
FTW_DNR 不可读取的目录,此目录以下将不被遍历
FTW_SL 符号连接
FTW_NS 无法取得stat结构数据,有可能是权限问题
最后一个参数depth代表ftw()在进行遍历目录时同时打开的文件数。ftw()在遍历时每一层目录至少需要一个文件描述词,如果遍历时用完了depth所给予的限制数目,整个遍历将因不断地关文件和开文件操作而显得缓慢
如果要结束ftw()的遍历,fn()只需返回一非零值即可,此值同时也会是ftw()的返回值。否则ftw()会试着走完所有的目录,然后返回0
返 回 值:遍历中断则返回fn()函数的返回值,全部遍历则返回0,若有错误发生则返回-1
附加说明:由于ftw()会动态配置内存使用,请使用正常方式(fn函数返回非零值)来中断遍历,不要在fn函数中使用longjmp()
示例:
/*列出/etc/X11目录下的子目录*/
#include <sys/stat.h>
#include <unistd.h>
#include <ftw.h>
int fn(const char *file, const struct stat *sb, int flag)
{
if(flag == FTW_D)
printf("%s --- directory\n", file);
else
printf("%s \n",file);
return 0;
}
int main()
{
ftw("/etc/X11",fn,500);
}
F. c语言opendir打开文件夹,完整程序
#include<stdio.h>
#include <sys/types.h>
#include <dirent.h>
int main()
{
DIR *dfd;
dfd = opendir("文件夹名称");
closedir(dfd);
//想读取文件夹,用readdir
return 0;
}
G. C语言怎么读取某一文件夹下的所有文件夹和文件
读取的代码方式如下:
int main()
{
long file;
struct _finddata_t find;
_chdir("d:\");
if((file=_findfirst("*.*", &find))==-1L)
{
printf("空白! ");
exit(0);
}
printf("%s ", find.name);
while(_findnext(file, &find)==0)
{
printf("%s ", find.name);
}
_findclose(file);
return 0;
}
H. C语言如何读取指定路径下的所有指定格式的文件
用C语言读取目录中的文件名的方法:
1、如果是在window环境下,可以用一下方法:
使用stdlib.h头文件声明的system()函数
_CRTIMP int __cdecl system (const char*);
system("dir c:\ /a:h /b > c:\dir.txt");
调用系统命令dir,把c:目录下文件列表写入文件dir.txt中
2、使用dirent.h头文件中声明的opendir(),readdir()函数;
intmain(intargc,char*argv[])
{
DIR*directory_pointer;
structdirent*entry;
if((directory_pointer=opendir("d:\XL"))==NULL)
printf("Erroropening ");
else
{
while((entry=readdir(directory_pointer))!=NULL)
{
printf("%s ",entry->d_name);
}
closedir(directory_pointer);
}
system("PAUSE");
return0;
}
3、如果没有dirent.h,可以使用io.h头文件中声明的_findfirst(),_findnext()函数;
示例代码:
intmain(intargc,char*argv[])
{
longfile;
struct_finddata_tfind;
_chdir("d:\");
if((file=_findfirst("*.*",&find))==-1L)
{
printf("空白! ");
exit(0);
}
printf("%s ",find.name);
while(_findnext(file,&find)==0)
{
printf("%s ",find.name);
}
_findclose(file);
system("PAUSE");
return0;
}
I. C语言中 DIR * (*real_opendir)(const char *name) = NULL; 这样书写的格式和意义是什么呢
这是声明一个函数指针
函数指针的声明方法为:
函数类型(标志符指针变量名)(形参列表);
注1:“函数类型”说明函数的返回类型,“(标志符指针变量名)”中的括号不能省,若省略整体则成为一个函数说明,说明了一个返回的数据类型是指针的函数,后面的“形参列表”表示指针变量指向的函数所带的参数列表。例如:
intfunc(intx);/*声明一个函数*/
int(*f)(intx);/*声明一个函数指针*/
J. 怎样使用C语言列出某个目录下的文件
C语言本身没有提供象dir_list()这样的函数来列出某个目录下所有的文件。不过,利用C语言的几个目录函数,你可以自己编写一个dir_list()函数。 首先,头文件dos.h定义了一个find_t结构,它可以描述DOS下的文件信息,包括文件名、时间、日期、大小和属性。其次,C编译程序库中有_dos_findfirst()和_dos_findnext()这样两个函数,利用它们可以找到某个目录下符合查找要求的第一个或下一个文件。 dos_findfirst()函数有三个参数,第一个参数指明要查找的文件名,例如你可以用“*.*”指明要查找某个目录下的所有文件。第二个参数指明要查找的文件属性,例如你可以指明只查找隐含文件或子目录。第三个参数是指向一个find_t变量的指针,查找到的文件的有关信息将存放到该变量中。 dos_findnext()函数在相应的目录中继续查找由_dos_findfirst()函数的第一个参数指明的文件。_dos_findnext()函数只有一个参数,它同样是指向一个find_t变量的指针,查找到刚文件的有关信息同样将存放到该变量中。 利用上述两个函数和find_t结构,你就可以遍历磁盘上的某个目录,并列出该目录下所有的文件,请看下例: #include <stdio.h> #include <direct.h> #include <dos.h> #include <malloc.h> #include <memory.h> #include <string.h> typedef struct find_t FILE_BLOCK void main(void); void main(void){FILE_BLOCK f-block; /* Define the find_t structure variable * / int ret_code; / * Define a variable to store the return codes * / / * Use the "*.*" file mask and the 0xFF attribute mask to list all files in the directory, including system files, hidden files, and subdirectory names. * / ret_code = _dos_findfirst(" *. * ", 0xFF, &f_block); /* The _dos_findfirst() function returns a 0 when it is successful and has found a valid filename in the directory. * / while (ret_code == 0){/* Print the file's name * / printf(" %-12s\n, f_block, name); / * Use the -dos_findnext() function to look