The code bellow extracts the file name of a file using its path.
#include <iostream>
#include <vector>
using namespace std;
int main() {
cout << "Program operating..." << endl;
string s = "C:\\Users\\user\\Pictures\\Strategic_Offense_Logo_1";
string name;
for (unsigned int i = s.size() - 1; i > 0; i--) {
if (s[i] == '\\') {
for (unsigned int j = i + 1; j < s.size(); j++) {
if (s[j] == '.' || j == s.size() - 1) {
if (j == s.size() - 1)
j++;
vector<char> v(j - i - 1);
unsigned int l = 0;
for (unsigned int k = i + 1; k < j; k++) {
v[l++] = s[k];
}
char* vectorAsArray = &v[0];
name = vectorAsArray;
name.resize(v.size());
break;
}
}
break;
}
}
cout << name << endl;
}
#include <iostream>
#include <vector>
using namespace std;
int main() {
cout << "Program operating..." << endl;
string s = "C:\\Users\\user\\Pictures\\Strategic_Offense_Logo_1.png";
string name;
for (unsigned int i = s.size() - 1; i > 0; i--) {
if (s[i] == '\\') {
for (unsigned int j = i + 1; j < s.size(); j++) {
if (s[j] == '.' || j == s.size() - 1) {
if (j == s.size() - 1)
j++;
vector<char> v(j - i - 1);
unsigned int l = 0;
for (unsigned int k = i + 1; k < j; k++) {
v[l++] = s[k];
}
char* vectorAsArray = &v[0];
name = vectorAsArray;
name.resize(v.size());
break;
}
}
break;
}
}
cout << name << endl;
}
Is there a more efficient way to do this?
Purpose: I am making a texture class and I want to refer the texture by its name instead of its path or a made up ID.
Note: library does not work at all with the visual studio compiler. If you know how to fix it or have an alternative solution, please post.