user1615186
user1615186

Reputation:

Why not able to apply str.find_last_of( ) on const char * str

I am just trying this code... I want to try to split my path as directory and file.

#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);

}

and I am getting these errors

../src/test.cpp:20: error: request for member ‘find_last_of’ in ‘str’, which is of non-class type ‘const char*’ ../src/test.cpp:21: error: request for member ‘substr’ in ‘str’, which is of non-class type ‘const char*’ ../src/test.cpp:22: error: request for member ‘substr’ in ‘str’, which is of non-class type ‘const char*’

Can anyone tell me how can I make it work by passing a character pointer to the SplitFilename() function.

Upvotes: 0

Views: 1641

Answers (1)

anio
anio

Reputation: 9171

str is a const char*. It is not a string. char is a primitive type. Not an object. I think you meant to convert str into an actual string object:

std::string my_string(str);
my_string.find_last_fo("//"):
.
.
.

Upvotes: 1

Related Questions