//一些函数,记录下来,方便以后查找#include "stdafx.h"
#include <windows.h>
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
const int bufSize = 512;
char buf[bufSize];
ZeroMemory(buf, bufSize);
if (GetModuleFileName(NULL, buf, bufSize));
{
cout<<buf<<endl;
}
getchar();
return 0;
}
上面的函数运行结果为返回该exe的绝对路径。
下面一个函数会递归的输出输入的路径下的所有文件(输出形式为完整的绝对路径)
// ConsoleApplication1.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <windows.h>
#include <iostream>
#include <string>
#include <io.h>
#include <vector>
using namespace std;
void getFiles( string path, vector<string>& files )
{
//文件句柄
long hFile = 0;
//文件信息
struct _finddata_t fileinfo;
string p;
if((hFile = _findfirst(p.assign(path).append("\\*").c_str(),&fileinfo)) != -1)
{
do
{
//如果是目录,迭代之
//如果不是,加入列表
if((fileinfo.attrib & _A_SUBDIR))
{
if(strcmp(fileinfo.name,".") != 0 && strcmp(fileinfo.name,"..") != 0)
getFiles( p.assign(path).append("\\").append(fileinfo.name), files );
}
else
{
files.push_back(p.assign(path).append("\\").append(fileinfo.name) );
}
}while(_findnext(hFile, &fileinfo) == 0);
_findclose(hFile);
}
}
int _tmain(int argc, _TCHAR* argv[])
{
string filepath;
vector<string> files;
while(cin>>filepath)
{
getFiles(filepath, files);
for(string name : files)
{
cout<<name<<endl;
}
}
return 0;
}
本文介绍两个实用的C++函数:一个用于获取当前执行文件的绝对路径;另一个用于递归地列出指定目录下所有文件的完整路径。
2万+

被折叠的 条评论
为什么被折叠?



