Fetch the project current working directory and fetch EXE directory

Source: Internet
Author: User
Tags define function

(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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.