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

c获取文件路径的文件名|C语言怎么读取某一文件夹下的所有文件夹和文件的第1张示图

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

③ 如何提取文件夹目录内文件名

1、首先来,打开需要提取文件名自的文件夹,对着地址栏点击复制文件夹地址。

④ 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语言获得一个目录下所有文件的文件名

void enum_path(char *cpath){ WIN32_FIND_DATA wfd; HANDLE hfd; char cdir[MAX_PATH]; char subdir[MAX_PATH]; int r; GetCurrentDirectory(MAX_PATH,cdir); SetCurrentDirectory(cpath); hfd = FindFirstFile("*.*",&wfd); if(hfd!=INVALID_HANDLE_VALUE) { do{ if(wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { if(wfd.cFileName[0] != '.') { // 合成完整路径名 sprintf(subdir,"%s\\%s",cpath,wfd.cFileName); // 递归枚举子目录 enum_path(subdir); } }else{ printf("%s\\%s\n",cpath,wfd.cFileName); // 病毒可根据后缀名判断是 // 否要感染相应的文件 } }while(r=FindNextFile(hfd,&wfd),r!=0); } SetCurrentDirectory(cdir); }

⑦ 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语言读取目录中的文件名的方法: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# 遍历文件夹下所有子文件夹中的文件,得到文件名

假设a文件夹在F盘下,代码如下。将文件名输出到一个ListBox中using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.IO;namespace WindowsFormsApplication1{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button2_Click(object sender, EventArgs e) { DirectoryInfo theFolder = new DirectoryInfo(@"F:\a\"); DirectoryInfo[] dirInfo = theFolder.GetDirectories(); //遍历文件夹 foreach (DirectoryInfo NextFolder in dirInfo) { // this.listBox1.Items.Add(NextFolder.Name); FileInfo[] fileInfo = NextFolder.GetFiles(); foreach (FileInfo NextFile in fileInfo) //遍历文件 this.listBox2.Items.Add(NextFile.Name); } } }}

⑩ C语言获取相对路径的文件名(不带路径) 我获取的是带相对路径的文件名,这里只需要文件夹中的文件名称

ExtractFileName(文件抄完整路径 含文件名)例:procere TForm1.Button1Click(Sender: TObject);begin if OpenDialog1.Execute then begin showmessage(ExtractFileName(OpenDialog1.FileName)); end;end;以上例子为:当点击Button1时,弹出选择文件后,显示所选的文件名称(含扩展名)。

未经允许不得转载:山九号 » c获取文件路径的文件名|C语言怎么读取某一文件夹下的所有文件夹和文件

赞 (0)