如何从 C 获取 Linux 中当前文件 (pwd) 的路径?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16285623/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the to get path to the current file (pwd) in Linux from C?
提问by
I'd like to know if it is somehow possible to run system("pwd")
on current DIR. So for example let's have this folder structure.
我想知道是否可以system("pwd")
在当前 DIR上运行。例如,让我们拥有这个文件夹结构。
example
>test
>>file
>test2
>>file3
>>file4
And with opendir()
and readdir()
I'll get to the file3
and I want to use system("pwd")
to get path: ..../example/test2/file3
. Is this somehow possible or pwd
will return path to the main.c
all the time?
使用opendir()
and readdir()
I'll get to file3
and I want to use system("pwd")
to get path: ..../example/test2/file3
. 这是可能的还是pwd
会一直返回路径main.c
?
采纳答案by Sujoy
Simply opening and reading directories does not change the current working directory. However, changing directory in your program will.
简单地打开和读取目录不会改变当前的工作目录。但是,更改程序中的目录会。
for reference,
以供参考,
#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);
}
回答by ST3
When you use system(...) call with Windows and Linux it just executes one command. It is possible to do the same using file with commands (you can create it with C code), but my oppinion is, that you should use nftw() to get dirrectories and after that use opendir()/readdir().
当您在 Windows 和 Linux 上使用 system(...) 调用时,它只执行一个命令。可以使用带有命令的文件来做同样的事情(你可以用 C 代码创建它),但我的意见是,你应该使用 nftw() 来获取目录,然后使用 opendir()/readdir()。
回答by jbr
回答by PADYMKO
For POSIX systems I found three solutions:
对于 POSIX 系统,我找到了三个解决方案:
Get value from an environment variables "PWD"
从环境变量“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;
}
Result:
结果:
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
Use getcwd()
使用 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;
}
Result
结果
Path info by use getcwd():
Workdir: /media/setivolkylany/WorkDisk/Programming/Projects/c-utils
Filepath: /media/setivolkylany/WorkDisk/Programming/Projects/c-utils/main.c
Execute system command "pwd" and read output
执行系统命令“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;
}
Result:
结果:
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