[英]How to get the path to the current file (pwd) in Linux from C?
我想知道是否有可能在當前 DIR 上運行system("pwd")
。 例如,讓我們擁有以下文件夾結構:
example
>test
>>file
>test2
>>file3
>>file4
使用opendir()
和readdir()
我將到達file3
,我想使用system("pwd")
來獲取路徑..../example/test2/file3
。 這是可能的,還是pwd
返回main.c
的路徑?
簡單地打開和讀取目錄不會改變當前的工作目錄。 但是,更改程序中的目錄會。
以供參考,
#include <unistd.h>
#include <stdio.h>
int main() {
char cwd[1024];
chdir("/path/to/change/directory/to");
getcwd(cwd, sizeof(cwd));
printf("Current working dir: %s\n", cwd);
}
對於 POSIX 系統,我找到了三個解決方案:
從環境變量“PWD”中獲取值
#include <stdio.h>
#include <stdlib.h>
#ifdef __unix__
#define IS_POSIX 1
#else
#define IS_POSIX 0
#endif
int main (int argv, char **argc)
{
if (IS_POSIX == 1) {
puts("Path info by use environment variable PWD:");
printf("\tWorkdir: %s\n", getenv("PWD"));
printf("\tFilepath: %s/%s\n", getenv("PWD"), __FILE__);
}
return 0;
}
結果:
Path info by use environment variable PWD:
Workdir: /media/setivolkylany/WorkDisk/Programming/Projects/c-utils
Filepath: /media/setivolkylany/WorkDisk/Programming/Projects/c-utils/main.c
使用 getcwd()
#include <stdio.h>
#include <stdlib.h>
#ifdef __unix__
#define IS_POSIX 1
#include <unistd.h>
#else
#define IS_POSIX 0
#endif
int main (int argv, char **argc)
{
if (IS_POSIX == 1) {
char cwd[1024];
getcwd(cwd, sizeof(cwd));
puts("Path info by use getcwd():");
printf("\tWorkdir: %s\n", cwd);
printf("\tFilepath: %s/%s\n", cwd, __FILE__);
}
return 0;
}
結果
Path info by use getcwd():
Workdir: /media/setivolkylany/WorkDisk/Programming/Projects/c-utils
Filepath: /media/setivolkylany/WorkDisk/Programming/Projects/c-utils/main.c
執行系統命令“pwd”並讀取輸出
#ifdef __unix__
#define IS_POSIX 1
#define _BSD_SOURCE
#else
#define IS_POSIX 0
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main (int argv, char **argc)
{
if (IS_POSIX == 1) {
char buffer[500];
FILE *output;
// read output of a command
output = popen("/bin/pwd", "r");
char *pwd = fgets(buffer, sizeof(buffer), output);
// strip '\n' on ending of a line
pwd = strtok(pwd, "\n");
puts("Path info by execute shell command 'pwd':");
printf("\tWorkdir: %s\n", pwd);
printf("\tFilepath: %s/%s\n", pwd, __FILE__);
}
return 0;
}
結果:
Path info by execute shell command 'pwd':
Workdir: /media/setivolkylany/WorkDisk/Programming/Projects/c-utils
Filepath: /media/setivolkylany/WorkDisk/Programming/Projects/c-utils/main.c
當您在 Windows 和 Linux 上使用 system(...) 調用時,它只執行一個命令。 可以使用帶有命令的文件來做同樣的事情(你可以用 C 代碼創建它),但我的意見是,你應該使用 nftw() 來獲取目錄,然后使用 opendir()/readdir()。
如何不使用pathconf
硬編碼路徑長度
我相信這是正確的方法:
#define _XOPEN_SOURCE 700
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
int main(void) {
long n;
char *buf;
n = pathconf(".", _PC_PATH_MAX);
assert(n != -1);
buf = malloc(n * sizeof(*buf));
assert(buf);
if (getcwd(buf, n) == NULL) {
perror("getcwd");
exit(EXIT_FAILURE);
} else {
printf("%s\n", buf);
}
free(buf);
return EXIT_SUCCESS;
}
編譯並運行:
gcc -Wall -Wextra -std=c11 -pedantic-errors -o getcwd.out getcwd.c
./getcwd.out
POSIX 將_PC_PATH_MAX
描述為:
為變量 {PATH_MAX} 返回的值表示如果指定目錄是進程的當前工作目錄,則可以給出的最長相對路徑名。 如果路徑名中的子目錄跨入限制性更強的文件系統,進程可能並不總是能夠生成那么長的名稱並使用它。
在 Ubuntu 18.10 上測試,在這個實現中n == 4096
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.