C++ 删除、重命名文件 、获取当前目录所有文件代码
#include <iostream>
#include <io.h>
#include <string>
using namespace std;
void dir(string path)
{
long hFile = 0;
struct _finddata_t fileInfo;
string pathName, exdName;
if ((hFile = _findfirst(pathName.assign(path).append("\\*").c_str(), &fileInfo)) == -1) {
return;
}
int count = 0;
do {
//cout << fileInfo.name << (fileInfo.attrib&_A_SUBDIR? "[folder]":"[file]") << endl; //用于打印当前所有的文件名(此处还包含主目录)
cout <<count<<":"<<fileInfo.name <<endl;
count++;
} while (_findnext(hFile, &fileInfo) == 0);
_findclose(hFile);
return;
}
int main()
{
string path="H://test";//测试路径
remove("H://test/a.txt");//删除文件
rename("H://test/b.txt","H://test/c.txt");//将b.txt重命名为c.txt
dir(path);
remove("H://test/a.txt");
getchar();
return 0;
}