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