fname from FString

In C++, the fname function from the FString class is used to retrieve the filename associated with a given path. The function takes no arguments and returns a string representing the filename.

Here is a step-by-step explanation of how the fname function works:

  1. The fname function is a member function of the FString class. This means it can be called on an instance of the FString class using the dot operator.

  2. When the fname function is called, it internally accesses the private data member of the FString class that stores the path.

  3. The function extracts the filename from the path by removing everything before the last occurrence of the path separator character. In most operating systems, the path separator is a forward slash ("/") or a backslash ("\").

  4. The extracted filename is then returned as a string.

Here is an example usage of the fname function:

#include <iostream>
#include <string>

class FString {
private:
    std::string path;

public:
    FString(const std::string& p) : path(p) {}

    std::string fname() {
        size_t lastSeparator = path.find_last_of("\\/");
        if (lastSeparator != std::string::npos) {
            return path.substr(lastSeparator + 1);
        }
        return path;
    }
};

int main() {
    FString filePath("/path/to/file.txt");
    std::string fileName = filePath.fname();
    std::cout << "Filename: " << fileName << std::endl;

    return 0;
}

In this example, we create an instance of the FString class with the path "/path/to/file.txt". We then call the fname function on this instance, which returns the filename "file.txt". Finally, we print the filename to the console.