ysap ysap - 2 months ago 6
C Question

Extract file name from full path in C using MSVS2005

In a C program, I have a file path in a string (specifically, this is the

exe
name stored in
argv[0]
). I would like to extract the file name and discard the directory path using MS Visual Studio 2005. Any built in function for that?

Answer

For reference, here's the code I implemented, supposedly Win/Unix compatible:

    char *pfile;
    pfile = argv[0] + strlen(argv[0]);
    for (; pfile > argv[0]; pfile--)
    {
        if ((*pfile == '\\') || (*pfile == '/'))
        {
            pfile++;
            break;
        }
    }