使函数接受字符串变量内的路径
making function accept the path that's inside the string variable
我有一个名为b1
的字符串变量。
该变量保存路径。
我需要让"删除"函数从变量中获取该路径b1
这是我的代码...
#include <sys/types.h>
#include <dirent.h>
#include <errno.h>
#include <vector>
#include <string>
#include <iostream>
using namespace std;
string a1 = "W:\ftp\4CS\DL\";
string a2;
int getdir (string dir, vector<string> &files)
{
DIR *dp;
struct dirent *dirp;
if((dp = opendir(dir.c_str())) == NULL)
{
cout << "Error(" << errno << ") opening " << dir << endl;
return errno;
}
while ((dirp = readdir(dp)) != NULL) {
std::string fname = dirp->d_name;
if(fname.find("te") != std::string::npos)
files.push_back(fname);
}
}
int main()
{
string catcher;
string dir = string(a1);
vector<string> files = vector<string>();
getdir(dir,files);
for (unsigned int i = 0; i < files.size();)
{
//cout << files[i] << endl;
a2 = files[i];
cout << a2 << endl;
string b1 = a1 + a2;
cout << b1 << endl;
//const int result = remove("W:\ftp\4CS\DL\testng it.txt");
char filename[] = b1;
/* Deletes the file if exists */
if (remove(filename) != 0)
perror("File deletion failed");
else
cout << "File deleted successfully";
return 0;
i++;
}
system("pause");
return 0;
}
简单
remove(b1.c_str());
调用 c_str(( 从C++字符串中获取指向 C 字符串的指针。
相关文章:
- boost xml parsingl将xml的路径作为变量发送
- 从 C 中的变量获取文件的路径,C++
- 如何为文件路径使用变量?
- CMake 中的路径变量
- 在 Visual Studio 2010(及更高版本)中定义包含或链接路径变量的位置
- 局部变量 'sp' 在被读取之前在所有路径中重新分配
- 如何更改变量文件名的文件路径?
- 如何从文件中存在的路径中检索环境变量
- 如何将文件路径分配给C 中的变量
- QT;我如何在运行时存储通往变量的资源路径
- 使函数接受字符串变量内的路径
- 我正在尝试在Eclipse中构建一个C 项目,但是即使在使用MINGW路径设置路径变量之后,也会获得构建错误
- 从变量字符串中提升文件系统路径
- 当InProcServer32键定义DLL的完整路径时,path环境变量中为什么需要COM DLL的路径
- 无法使用 CMake 变量PROJECT_SOURCE_DIR定义静态库的绝对路径
- 我如何克服无声无法更改VC 2013中的路径环境变量
- 最短路径,带有两个变量
- 为什么ShellExecute不能使用包含文件路径的LPCSTR变量
- 代码块编译的exe链接到一个没有系统路径变量(C++)的dll
- visualstudio2013在运行.exe时修改路径变量