當前位置:首頁 » 文件管理 » c語言獲取文件夾

c語言獲取文件夾

發布時間: 2022-09-06 19:39:30

1. c語言 怎樣獲取文件夾中的所有文件

較簡單的是用DOS命令 DIR 並轉向到一個文件,再打開文件讀出一個一個文件名。
例如:
char my_cmd[80] = "DIR/B/A-D D:\\USER\\WANG >> abc.lis";
system( cmd);
你就獲得D:\\USER\\WANG 文件夾中的所有文件,選項意思是 只列 文件名,並按字母排列。
>> abc.lis 轉向,存入文件 abc.lis
接著,你可以 用FILE *fp; fp = fopen("abc.lis","r"); 打開文件
用 fgets() 讀文件名。

2. c語言怎麼打開一個文件夾,求完整程序

如果是源文件的話,單擊右鍵,找到並打開「屬性」,屬性的「常規」裡面有打開方式,你單擊「打開方式」後面的「更改」,然後再「推薦程序"與"其他程序里"找,如果沒有,那麼點擊下面的「瀏覽」,這一步是要找到你安裝turbo C&C++的目錄下你turbo c&C++這個應用程序的圖標,這圖標一般跟你安裝後出現在桌面的圖標相似。(如果你是默認安裝的,那麼你一般可以去c盤,program files文件夾,然後可能會看到名字含有turbo 單詞的文件夾,在裡面也許你可以直接看到那個應用程序圖標,也許不能,不能的話,那就在裡面有個bin文件夾的,bin就是二進制的意思,bin文件夾里一般含有這個應用程序的圖標。你選中後摁下面的「打開」就行了。
你是初中生嗎,怎麼還用turbo c呢,是用的譚浩強的教材吧,他的教材其實裡面有些源程序錯誤挺多的。

3. 怎麼用c語言讀取電腦裡面的一個文件夾,並篩選同類型文件,比如exe文件。然後運行或刪除

1、如果自己寫的話,需要了解系統的API函數,以WINDOWS來說即FindFile相關的那些函數。 通過遞歸來搞。

2、還有就是直接執行dir命令(win)、linux平台 可以執行ls命令。 根據需要進行操作即可。

4. c語言中如何得到當前文件所在位置

如果是通過open方式打開的,那麼第一個參數就是文件路徑信息:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int open(const char *path, int oflag, /* mode_t mode */...);

如果是通過fopen方式打開的,那麼第一個參數就是文件路徑信息:
#include <stdio.h>
FILE *fopen(const char *filename, const char *mode);

無論通過open還是fopen打開文件,都必須先知道文件路徑信息,盡管可能是相對路徑。
如果知道了filename的內容,我們就可以定位它的絕對路徑,也就是你說的完全路徑。

1. filename本身就是絕對路徑,ok。
2. filename是相對路徑,那麼先通過getcwd獲取進程的執行路徑,然後再獲取絕對路徑即可。
#include <unistd.h>
extern char *getcwd(char *buf, size_t size);

但是,如果進程在打開文件後又執行了chdir、fchdir之類函數的話,估計就不能夠再獲取文件路徑信息了。
#include <unistd.h>
int chdir(const char *path);
int fchdir(int fildes);

5. c語言如何讀取文件夾

這個問題根據環境的不同有不同回答。
Windows下:
需要使用CAtlTransactionManager Class 來進行這類操作,具體內容十分復雜,請參考(http://msdn.microsoft.com/zh-cn/library/dd795992.aspx)。
Linux下:
使用dirent.h頭文件進行這類操作。請參考(http://ke..com/link?url=5amOVqTNt1MuhIKq)。

6. C語言怎麼列出指定文件夾或者分區裡面的文件和文件夾

//以下是c++方法
#include<stdio.h>
#include<io.h>
#include<string.h>
#include<string>
#include<iostream>
usingnamespacestd;

#defineWIDTH300
#defineH40

intlen=0;
stringfiles[1000];
voidgetFiles(stringpath,intdeepth)
{
longhFile=0;
struct_finddata_tfileinfo;
stringp;
if((hFile=_findfirst(p.assign(path).append("\*").c_str(),&fileinfo))!=-1)
{
do
{
if(fileinfo.attrib&_A_SUBDIR)
{
//想取子文件夾里的內容,這里可以用以下代碼,deepth控制深度
//getFiles(path+"/"+fileinfo.name,deepth+1);
}
else
{
files[len]=fileinfo.name;
++len;
}
}while(_findnext(hFile,&fileinfo)==0);
_findclose(hFile);
}
}

stringpath="E:/";

voidmain()
{
inti,res;
getFiles(path,0);
for(i=0;i<len;++i)
puts(files[i].c_str());
Sleep(800);
return;
}

7. 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);

}

}

測試結果如圖所示。

8. 如何用C語言獲取目錄下的文件和目錄列表

1、可以利用getenv函數來實現。
在Linux系統中,home目錄的定義是通過系統環境變數中的HOME變數值來確定的,在shell下可以通過
echo $HOME來查看。
而在C語言中,庫函數getenv可以用作獲取環境變數值。該函數位於stdlib.h, 原型為
char *getenv(char *name);
功能為獲取名字為name的環境變數字元串。
所以,下面代碼就可以獲取到home目錄名了:
2、常式:
char *home;
home = getenv("HOME");
printf("the home path is %s\n", home);

9. C語言問題 已知某一路徑,如何得到該路徑下的某一文件夾的路徑

#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include <io.h>

#define MAX_FILE_PATH_LEN 128
#define INVALID_HANDLE -1

int main()
{
char aucFilePath[MAX_FILE_PATH_LEN + 1] = {0};
char aucTempPath[MAX_FILE_PATH_LEN + 1] = {0};
char *pFilePath = NULL;
int iDirNum = 0;
int iFileNum = 0;

_finddata_t finddata = {0};
long findhandle = INVALID_HANDLE;

//獲取當前目錄,也可以做入參傳入aucFilePath
pFilePath = getcwd(aucFilePath, MAX_FILE_PATH_LEN);
if (NULL == pFilePath)
{
printf("get current working directory fail, \n"
"errno = %d, errro description : %s\n", errno, strerror(errno));
getchar();
return -1;
}

printf("current working directory = %s\n", aucFilePath);
strncpy(aucTempPath, aucFilePath, strlen(aucFilePath));
strncat(aucTempPath, "\\*", MAX_FILE_PATH_LEN);

findhandle = _findfirst(aucTempPath, &finddata);
if(findhandle != INVALID_HANDLE)
{
do {
if(finddata.attrib & _A_SUBDIR)
{
if ((0 != strcmp(finddata.name, ".")) && (0 != strcmp(finddata.name, "..")))
{
//子目錄個數
iDirNum++;
printf("sub directory name = %s, path = %s\\%s, iDirNum = %d\n",
finddata.name, aucFilePath, finddata.name, iDirNum);
}
}
else
{
//子文件個數
iFileNum++;
printf("file = %s, iFileNum = %d\n", finddata.name, iFileNum);
}
}while(0 == _findnext(findhandle, &finddata));
_findclose(findhandle);
}

getchar();
return 0;
}

熱點內容
校園安全拍攝腳本公安 發布:2024-10-12 01:59:19 瀏覽:441
雲基存儲產業投資深圳 發布:2024-10-12 01:42:42 瀏覽:305
有保qq怎麼改密碼 發布:2024-10-12 01:26:48 瀏覽:844
sqlserver2005數據恢復 發布:2024-10-12 01:04:09 瀏覽:516
遠程訪問磁碟 發布:2024-10-12 00:59:16 瀏覽:476
glc低配有哪些配置 發布:2024-10-12 00:38:23 瀏覽:951
網關無法解析伺服器的DNS地址 發布:2024-10-12 00:33:38 瀏覽:282
windowsmysql自動備份腳本 發布:2024-10-12 00:33:37 瀏覽:188
寫短視頻的腳本的人 發布:2024-10-12 00:33:36 瀏覽:964
雲存儲免費的攝像頭 發布:2024-10-12 00:15:47 瀏覽:622