我只是在尝试这段代码......我想尝试将我的路径拆分为目录和文件。
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
using namespace std;
void SplitFilename (const char * str)
{
size_t found;
cout << "Splitting: " << str << endl;
found=str.find_last_of("//");
cout << " folder: " << str.substr(0,found) << endl;
cout << " file: " << str.substr(found+1) << endl;
}
int main() {
char *outFile1 = NULL;
outFile1 = "//tmp//Softwares//v//vdisk";////tmp//iscsi//target1//lun1
char* outFile2 = (char*) malloc(strlen(outFile1) + strlen(".meta") + 1);
strcpy(outFile2,outFile1);
strcat(outFile2, ".meta");
cout << "str2:" << outFile2 << "\n";
SplitFilename (outFile2);
}
我收到了这些错误
../src/test.cpp:20:错误:在“str”中请求成员“find_last_of”,它是非类类型“const char *”../src/test.cpp:21:错误:请求对于'str'中的成员'substr',它是非类类型'const char *' ../src/test.cpp:22:错误:请求'str'中的成员'substr',它不是-class 类型'const char*'</p>
谁能告诉我如何通过将字符指针传递给 SplitFilename() 函数来使其工作。