当前位置:首页 » 文件管理 » 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;
}

热点内容
有保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 浏览:280
windowsmysql自动备份脚本 发布:2024-10-12 00:33:37 浏览:188
写短视频的脚本的人 发布:2024-10-12 00:33:36 浏览:964
云存储免费的摄像头 发布:2024-10-12 00:15:47 浏览:622
DA与脚本 发布:2024-10-12 00:14:22 浏览:255
如何看配置支不支持黑苹果 发布:2024-10-12 00:13:45 浏览:588