(1) Take the current working directory:
Related functions: Get_current_dir_name, GETWD, chdir
Header file: #include
Define function: char * GETCWD (char * buf, size_t size);
Function Description: GETCWD () copies the current working directory absolute path to the memory space referred to in parameter buf, and the parameter size is the space size of BUF.
Note:
1, when calling this function, buf refers to the memory space is large enough. If the string length of the absolute path of the working directory exceeds the size of the parameter size, then the value returned Null,errno is Erange.
2, if the parameter buf is NULL,GETCWD () will automatically configure memory according to the size of the parameter size (using malloc ()), if the parameter size is also 0, then GETCWD () will determine the size of the configured memory according to the string degree of the absolute path of the working directory. The process can use free () to release this space after using the string.
Return value: Successful execution copies the result to the memory space referred to by the parameter buf, or returns an automatically configured string pointer. The failure returns NULL, and the error code is stored in errno.
There are two versions: _GETCWD ()
_WGETCWD ()
Example
crt_getcwd.c// this program places the name of the current directory in the // buffer array, then displays the name of the current directory // on the screen. passing null as the buffer forces getcwd to allocate// memory for the path, which allows the code to support file paths// longer than _MAX_PATH, which are supported by NTFS. #include < direct.h> #include <stdlib.h> #include <stdio.h>int main ( void ) { char* buffer; // get the current working directory: if ( (BUFFER&NBSP;=&NBSP;_GETCWD ( NULL, 0 )) == null ) perror ( "_getcwd error" ); else { printf ( "%s \nlength: %d\n", buffer, strnlen (buffer) ); free (buffer); }}
(2) Fetch EXE directory:
DWORD WINAPI GetModuleFileName (_in_opt_ hmodule hmodule, _out_ LPTSTR lpFileName, _in_ DWORD nSize);
Example:
wchar_t Szcurpath[max_path] = {0}; GetModuleFileName (NULL, Szcurpath, MAX_PATH); MessageBox (Szcurpath);
This article is from the "whatever957" blog, make sure to keep this source http://whatever957.blog.51cto.com/6835003/1599487
Fetch the project current working directory and fetch EXE directory