Using _makepath()/_makepath_s(), _splitpath()/_splitpath_s()

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Header file: Standard

Additional library: none/default

Additional project setting: Set project to be compiled as C

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C Code (/TC)

Other info: none

To do: Create and split the path

To show: Using _makepath()/_makepath_s(), _splitpath()/_splitpath_s()

 

#include <stdlib.h>

#include <stdio.h>

 

int main(void)

{

char path_buffer[_MAX_PATH];

char drive[_MAX_DRIVE];

char dir[_MAX_DIR];

char fname[_MAX_FNAME];

char ext[_MAX_EXT];

size_t sizeInCharacters = 100;

/* _makepath(path_buffer, "g", "\\Testdir\\myexample\\", "testfile", "txt"); */

_makepath_s(path_buffer, sizeInCharacters, "g", "\\Testdir\\myexample\\", "testfile", "txt");

printf("\nPath created with _makepath_s(): %s\n", path_buffer);

/* _splitpath(path_buffer, drive, dir, fname, ext); */

_splitpath_s(path_buffer, drive, _MAX_DRIVE, dir, _MAX_DIR, fname, _MAX_FNAME, ext, _MAX_EXT);

 

printf("\nPath extracted with _splitpath_s():\n");

printf(" Drive: %s\n", drive);

printf(" Dir: %s\n", dir);

printf(" Filename: %s\n", fname);

printf(" Ext: %s\n", ext);

 

return 0;

}

 

Output example:

 

Path created with _makepath_s(): g:\Testdir\myexample\testfile.txt

Path extracted with _splitpath_s():

Drive: g:

Dir: \Testdir\myexample\

Filename: testfile

Ext: .txt

Press any key to continue . . .

 

 

C and C++ Programming Resources | C & C++ Code Example Index