『壹』 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時,彈出選擇文件後,顯示所選的文件名稱(含擴展名)。