Hello all,
I am very new to C++ and I am having a bit of difficulty using the substr function. I am trying to extract the filename without the extension from a string that contains the full filename and path. I have been trying to do the following:
string filename; // assigned a value in the constructor
and later...
int lastSlash = filename.find_last_of("\\");
int lastDot = filename.find_last_of(".");
int chars = lastDot - lastSlash;
string name = filename.substr(lastSlash, chars);
The 'filename' variable contains a valid string as I am managing to write to this file. I have also verified the values of 'lastSlash', 'lastDot' and 'chars' and these appear to be correct. However, the value that the field 'name' is receiving is '(null)'.
Can anyone explain what I may be doing wrong?
Many thanks in advance.
I am very new to C++ and I am having a bit of difficulty using the substr function. I am trying to extract the filename without the extension from a string that contains the full filename and path. I have been trying to do the following:
string filename; // assigned a value in the constructor
and later...
int lastSlash = filename.find_last_of("\\");
int lastDot = filename.find_last_of(".");
int chars = lastDot - lastSlash;
string name = filename.substr(lastSlash, chars);
The 'filename' variable contains a valid string as I am managing to write to this file. I have also verified the values of 'lastSlash', 'lastDot' and 'chars' and these appear to be correct. However, the value that the field 'name' is receiving is '(null)'.
Can anyone explain what I may be doing wrong?
Many thanks in advance.