C ++ equivalent of MATLAB file series function

MATLAB has a nice function called file parts, which takes the full path to the file and analyzes it for the path, file name (without extension) and extension, as in the following example from the documentation:

file = 'H:\user4\matlab\classpath.txt';

[pathstr, name, ext] = fileparts(file)

>> pathstr = H:\user4\matlab

>> name = classpath

>> ext = .txt

So, I was wondering if there is an equivalent function in any standard C ++ or C libraries that I could use? Or should I implement this myself? I understand this quite simply, but I wondered if there was something prepared in advance that would be preferable.

Thank.

+3
source share
3 answers

Some possible solutions, depending on your OS:

  • Visual C ++ _ splitpath function
  • Win32 , PathFindExtension, PathFindFileName, PathStripPath, PathRemoveExtension, PathRemoveFileSpec
+1

boost "basic_path", . , , boost Windows, Linux ..

+5

I just wrote this simple function. It behaves similarly to Matlab filepartsand works independently of the platform.

struct FileParts
{
    string path;
    string name;
    string ext;
};

FileParts fileparts(string filename)
{
    int idx0 = filename.rfind("/");
    int idx1 = filename.rfind(".");

    FileParts fp;
    fp.path = filename.substr(0,idx0+1);
    fp.name = filename.substr(idx0+1,idx1-idx0-1);
    fp.ext  = filename.substr(idx1);

    return fp;
}
+3
source

Source: https://habr.com/ru/post/1790060/


All Articles