当前位置:首页 » 编程语言 » c语言获取路径最后文件名
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

c语言获取路径最后文件名

发布时间: 2022-07-08 05:09:20

‘壹’ 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++获取未知文件文件名及路径

void main(argi,argv[])
{
print(argv[1]);
}
手机打代码真累

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

用system
调用
DOS
DIR
命令就可以了:
system
(
"dir
sss_*
/B
>
log.txt");
这就把
前缀为sss_的文抄件
文件名
存入
log.txt
文件了。
一个名字袭一行,没有别的东西。
你再
读出来。
#include
<stdio.h>
main()
{
FILE
*fp;
char
str[30][50];
//
假定文件数不超过30个
int
i,n=0;
system("dir
sss_*
/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语言获取文件名

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语言如何从路径名中分离文件名

声明一个足够长的名为fn的char型数组,调用库函数strrchr在含路径的全文件名中找到文件名前的'',将其后的文件名拷贝到fn中即可。举例代码如下:

//#include"stdafx.h"//Ifthevc++6.0,withthisline.
#include"stdio.h"
#include"string.h"
intmain(void){
charfn[30],*p;
charpathname[80]="e:\1\2\abc.dat";
//上句假设以某种方式获得的全文件名在pathname中,"..."中只是举例
strcpy(fn,(p=strrchr(pathname,'\'))?p+1:pathname);
//上句函数第2实参这样写以防止文件在当前目录下时因p=NULL而出错
printf("%s ",fn);//打出来看看
return0;
}

‘陆’ 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语言如何获取用户通过键盘输入的文件目录中的文件名和文件路径,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语言获得一个目录下所有文件的文件名

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语言:如何得到指定地址的文件夹中所有文件的文件名和其修改时间 包括子文件内的

俺前段时间写了段功能相似的程序,但用的是用C++/STL写的,访问目录使用了win32api(能访问指定目录的子目录)。

获取文件名与修改时间由FileOfDirectory::detectFiles实现(其实你只需要看这一个函数即可)。

这段程序以STL数组保存单个文件名,查询过程中没有回溯,wcsstr函数内部也是KMP,所以事实上这个程序也是按KMP查询的

安时间排序时使用STL算法库,时间复杂度同快速排序。

最后,这段代码是在VS2010编译的。

#include<vector>

#include<algorithm>

structFileNameAndTime

{

wchar_tszPath[MAX_PATH];//filedirectory

wchar_tszName[MAX_PATH];//filename

FILETIMElastAcc;//lastaccesstime

FileNameAndTime()

{

memset(&lastAcc,0,sizeof(lastAcc));

memset(szName,0,sizeof(wchar_t)*MAX_PATH);

memset(szPath,0,sizeof(wchar_t)*MAX_PATH);

}

FileNameAndTime(constPWCHARfn,constPWCHARpa,constLPFILETIMEft)

{

if((0==fn)||(0==pa)||(0==ft))

return;

memcpy(&lastAcc,ft,sizeof(lastAcc));

wcscpy(szName,fn);

wcscpy(szPath,pa);

}

FileNameAndTime(constFileNameAndTime&fnd)

{

memcpy(&this->lastAcc,&fnd.lastAcc,sizeof(this->lastAcc));

wcscpy(this->szName,fnd.szName);

wcscpy(this->szPath,fnd.szPath);

}

constFileNameAndTime&operator=(constFileNameAndTime&fnd)

{

if(this!=&fnd){

memcpy(&this->lastAcc,&fnd.lastAcc,sizeof(this->lastAcc));

wcscpy(this->szName,fnd.szName);

wcscpy(this->szPath,fnd.szPath);

}

return*this;

}

voidGetFullPath(wchar_t(&fp)[MAX_PATH])const

{

wcscpy(fp,szPath);

wcscat(fp,szName);

}

friendbooloperator>(constFileNameAndTime&l,constFileNameAndTime&r);//comparethisobjectbyaccesstime

};

booloperator<(constFileNameAndTime&l,constFileNameAndTime&r)//forsort

{

if(l.lastAcc.dwHighDateTime<r.lastAcc.dwHighDateTime)

returntrue;

elseif(l.lastAcc.dwHighDateTime==r.lastAcc.dwHighDateTime)

{

if(l.lastAcc.dwLowDateTime<r.lastAcc.dwLowDateTime)

returntrue;

}

returnfalse;

}

classFileOfDirectory

{

private:

staticconstwchar_tszDot[];

staticconstwchar_tszDotDot[];

staticconstwchar_tcStar;

staticconstwchar_tcSlash;

private:

std::vector<FileNameAndTime>vecFT;

wchar_tszCurrentPath[MAX_PATH];

private:

voidvalidatePath(constwchar_t*pPath)

{

wcscpy(szCurrentPath,pPath);

intlen=wcslen(szCurrentPath);

if((cStar!=szCurrentPath[len-1])

&&(cSlash!=szCurrentPath[len-2]))

{

szCurrentPath[len]=cSlash;

szCurrentPath[len+1]=cStar;

szCurrentPath[len+2]=0;

return;

}

if((cStar!=szCurrentPath[len-1])

&&(cSlash==szCurrentPath[len-2]))

{

szCurrentPath[len]=cStar;

szCurrentPath[len+1]=0;

return;

}

}

voiddetectFiles(constLPWSTRszDir)

{

WIN32_FIND_DATAffd;

HANDLEhFind=::FindFirstFile(szDir,&ffd);

if(INVALID_HANDLE_VALUE==hFind)

return;

do

{

if(ffd.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)

{

if((0==wcscmp(ffd.cFileName,szDot))||(0==wcscmp(ffd.cFileName,szDotDot)))

continue;

else

{

wchar_tszTempPath[MAX_PATH];

wcscpy(szTempPath,szDir);

szTempPath[wcslen(szTempPath)-1]=0;

wcscat(szTempPath,ffd.cFileName);

intlen=wcslen(szTempPath);

szTempPath[len]=cSlash;

szTempPath[len+1]=cStar;

szTempPath[len+2]=0;

detectFiles(szTempPath);

}

}

else{

wchar_tpath[MAX_PATH];

wcscpy(path,szDir);

path[wcslen(path)-1]=0;

vecFT.push_back(FileNameAndTime(ffd.cFileName,path,&ffd.ftLastAccessTime));

}

}

while(::FindNextFile(hFind,&ffd)!=0);

}

public:

FileOfDirectory(constLPWSTRszDir)

{

validatePath(szDir);

detectFiles(szCurrentPath);

}

voidSortByAccessTime()

{

sort(vecFT.begin(),vecFT.end());

}

intNumOfFiles()const{returnvecFT.size();}

intFindFilesByKeyWord(wchar_t*pszFn,int*outCome,intoutComeLen,boolbMatchAll=false)

{

wchar_tszTemp[MAX_PATH],szFnLwr[MAX_PATH];

intindex=0;

wcscpy(szFnLwr,pszFn);

_wcslwr(szFnLwr);

for(inti=0;i<vecFT.size();++i)

{

wcscpy(szTemp,vecFT[i].szName);

_wcslwr(szTemp);

if(true==bMatchAll)

{

if(0==wcscmp(szTemp,szFnLwr))

{

if(index>=outComeLen)

returnindex;

outCome[index++]=i;

}

}

else

{

if(0!=wcsstr(szTemp,szFnLwr))

{

if(index>=outComeLen)

returnindex;

outCome[index++]=i;

}

}

}

}

FileNameAndTimeGetItemByID(intindex)

{

if((index>=0)&&(index<vecFT.size()))

returnFileNameAndTime(vecFT[index]);

}

};

constwchar_tFileOfDirectory::szDot[]=L".";

constwchar_tFileOfDirectory::szDotDot[]=L"..";

constwchar_tFileOfDirectory::cStar=L'*';

constwchar_tFileOfDirectory::cSlash=L'\';

void__stdcallentp3()//测试程序

{

FileOfDirectoryfod(L"E:\game");

intids[256]={0};

fod.SortByAccessTime();

intlen=fod.FindFilesByKeyWord(L"main",ids,256);

for(inti=0;i<len;++i){

FileNameAndTimefnt(fod.GetItemByID(ids[i]));

CDbgString::OutputDbgStringW(L" %s%s",fnt.szPath,fnt.szName);

}

}

测试结果如图所示。

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

ExtractFileName(文件完整路径 含文件名)

例:
procere TForm1.Button1Click(Sender: TObject);
begin
if OpenDialog1.Execute then
begin
showmessage(ExtractFileName(OpenDialog1.FileName));
end;
end;

以上例子为:当点击Button1时,弹出选择文件后,显示所选的文件名称(含扩展名)。