文件管理 · 2022年9月10日

c读取文件名|C语言怎么读取某一文件夹下的所有文件夹和文件

⑴ C语言 如何通过文件指针获得文件名

在tc20中,一旦你成功打开一个文件,他将返回一个文件指针。

FILE*fp;

fp=fopen("abc.dat",文件状态(如w,r,r+));

当上面的操作成功后文件指针fp就会赋予你打开文件的最基本信息!

FILE结构在TurboC在stdio.h文件中有以下的文件类型声明:

typedefstruct

{

shortlevel;/*缓冲区“满”或“空”的程度*/

unsignedflags;/*文件状态标志*/

charfd;/*文件描述符(句柄)*/

unsignedcharhold;/*如无缓冲区不读取字符*/

shortbsize;/*缓冲区的大小*/

unsignedchar*buffer;/*数据缓冲区的位置*/

unsignedar*curp;/*指针,当前的指向*/

unsignedistemp;/*临时文件,指示器*/

shorttoken;/*用于有效性检查*/

}FILE;

为管理你打开的文件,操作系统为所有的文件创建一个打开文件信息的结构数组—文件控制块(FCB),而文件描述符就承担了访问与之对应的文件控制块的使命,他在c中就充当文件句柄。每一个文件都需要唯一的一个标识,这样才能管理若干个文件

FCB他存贮这你所有打开文件的信息,而只有通过文件句柄才能访问与之对应的FCB,从而访问你的文件.

文件句柄,就是FCB结构数组的下标

所以,通过文件指针获得文件名的操作路线:

FILE*fp;

charfd=fp->fd;

FCB*fcb;

char*filiname=fcb[fd].filiname

利用FCB(文件控制块)操作的例子见:

http://www.asme.net/blog/user/postcontent.jsp?neighborId=8747&kindLevel=1&kindId=24655&postId=40710&readSg=1

⑵ c语言获取文件名

voidget_filename(char*path,char*name){inti,j=0;for(i=0;path[i];i++)if(path[i]=='\')j=i;strcpy(name,&path[j]);}

这样得到的name就是你需要的。专

PS:对于属windows 路径中的是 而不是你题目中的/

⑶ c语言 读取目录中的文件名,并将其存入数组中

用system调用DOSDIR命令就可以了:system("dirsss_*/B>log.txt");这就把前缀为sss_的文抄件文件名存入log.txt文件了。一个名字袭一行,没有别的东西。你再读出来。#include<stdio.h>main(){FILE*fp;charstr[30][50];//假定文件数不超过30个inti,n=0;system("dirsss_*/B>log.txt");fp=fopen("log.txt","r");while(1){if(fgets(str[n],50,fp)==NULL)break;str[n][strlen(str[n])-1]='\0';//加一个字符串结束符n++;}fclose(fp);for(i=0;i<n;i++)printf("%s\n",str[i]);}

⑷ c语言中如何读文件名字

好像有函数,但是忘了,实在不行,你在C语言里调用DOS的dir命令写到一个文件里,然后再读那个文件。调用DOS为:system("dir/ad/b>fold.txt");/*这个是文件夹列表*/system("dir/a-d/b>file.txt");/*这个是文件列表*/你再读一下那两件文件。办法是有点笨。你自己再打开那两个文本文件看一下,里面的内容吧。

⑸ 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;}

⑹ 在c++中如何获取文件名

一、在不使用mfc的程序中获得某个目录下的所有文件名称,包括子目录。把文件名称以一个固定的程度放入一个缓冲中,这个缓冲要足够的大,能容下所有的文件名称。函数的输入为要查找的根目录,输出为存放所有文件名称的缓冲算法:使用递归二、代码:voidFindFileInDir(char*rootDir,char*strRet){charfname[MAC_FILENAMELENOPATH];ZeroMemory(fname,MAC_FILENAMELENOPATH);WIN32_FIND_DATAfd;ZeroMemory(&fd,sizeof(WIN32_FIND_DATA));HANDLEhSearch;charfilePathName[256];chartmpPath[256];ZeroMemory(filePathName,256);ZeroMemory(tmpPath,256);strcpy(filePathName,rootDir);BOOLbSearchFinished=FALSE;if(filePathName[strlen(filePathName)-1]!='\\'){strcat(filePathName,"\\");}strcat(filePathName,"*");hSearch=FindFirstFile(filePathName,&fd);//Isdirectoryif((fd.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)&&strcmp(fd.cFileName,".")&&strcmp(fd.cFileName,"..")){strcpy(tmpPath,rootDir);strcat(tmpPath,fd.cFileName);FindFileInDir(tmpPath,strRet);}elseif(strcmp(fd.cFileName,".")&&strcmp(fd.cFileName,"..")){sprintf(fname,"%-50.50s",fd.cFileName);strcat(strRet+strRet[strlen(strRet)],fname);}while(!bSearchFinished){if(FindNextFile(hSearch,&fd)){if((fd.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)&&strcmp(fd.cFileName,".")&&strcmp(fd.cFileName,"..")){strcpy(tmpPath,rootDir);strcat(tmpPath,fd.cFileName);FindFileInDir(tmpPath,strRet);}elseif(strcmp(fd.cFileName,".")&&strcmp(fd.cFileName,"..")){sprintf(fname,"%-50.50s",fd.cFileName);strcat(strRet+strRet[strlen(strRet)],fname);}}else{if(GetLastError()==ERROR_NO_MORE_FILES)//NormalFinished{bSearchFinished=TRUE;}elsebSearchFinished=TRUE;//TerminateSearch}}FindClose(hSearch);}

⑺ 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;

}

⑻ c语言如何获取用户通过键盘输入的文件目录中的文件名和文件路径,ballball大佬帮帮我🙏求代码

int main(){string s = "c:\\abc\\def\\text.txt";int xie_index = s.find_last_of('\\');制// 路径中最后一个\的位置 string file_dirname = s.substr(0, xie_index + 1);string file_basename = s.substr(xie_index + 1, s.size());cout << file_dirname << endl << file_basename << endl;}

⑼ c语言文件名提取

可以参考 DIR 命令选项 (/os /oe /od /on 等),知道其它排列方法。例如:system("dir *.* /os > m01.txt"); // m01.txt 存放内:按文件大小排列system("dir *.* /oe > m02.txt"); //m02.txt 存放:按文件扩展名次序容排列system("dir *.* /od > m03.txt"); //m03.txt 存放:按文件日期排列

⑽ 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;}