Getting the work path here introduces two methods:
1. Use the getcwd() function.
Header file: #include <>
Define function: char * getcwd(char * buf, size_t size);
Function description: getcwd() will copy the current working directory absolute path to the memory space referred to by parameter buf, and the parameter size is the space size of buf
Example:
void getPath() { char pPath[256] = {0}; getcwd(buf, 256); printf("filePath: %s\n", pPath); }
The main function calls this function and runs the result:
filePath: /home/myTest
2. Use the readlink() function.
Header file: #include <>
Function definition: int readlink(const char *path, char *buf, size_t bufsiz);
Function description: This function will connect the symbol of the parameter path to the memory space referred to by the parameter buf. The returned content does not end with NULL as a string, but will return the number of characters of the string. If the parameter bufsiz is less than the length of the content of the symbol connection, excessively long content will be truncated. Returns the buf length successfully, returns -1 if failed.
Example:
void getPath() { char pPath[256] = {0}; int ret = -1; ret = readlink("/proc/self/exe", pPath, 256); pPath[ret] = "\0" printf("the file Path: %s\n", pPath); } int main() { getPath(); }
Running results:
the file Path: /home/myTest/
The above example of obtaining the current working path under Linux is all the content I share with you. I hope you can give you a reference and I hope you can support me more.