C语言 执行dll时的当前工作目录

jhiyze9q  于 2023-01-01  发布在  其他
关注(0)|答案(1)|浏览(224)

我使用dll从C程序运行一些Fortran源代码。我想在Fortran中使用**CALL GETCWD(DIRNAME)**来访问文件。当前工作目录(CWD)是Fortran dll所在的目录还是C代码所在的目录?

kpbwa7wx

kpbwa7wx1#

  • CWD* 代表CcurrentWorkingDirectory,它(通常)是当前进程启动的目录。查看[Man7]: GETCWD(3)以了解更多细节。我准备了一个小示例以更好地理解所发生的事情。
  • 代码00.c *:
#include <errno.h>
#include <stdio.h>
#include <unistd.h>

#define PATH_SIZE 0x0200

int main()
{
    char buf[PATH_SIZE];
    if (getcwd(buf, PATH_SIZE) == NULL) {
        printf("Error %d getting CWD\n", errno);
        return 1;
    }
    printf("CWD: [%s]\n", buf);
    return 0;
}

输出

[cfati@cfati-5510-0:/mnt/e/Work/Dev/StackOverflow/q054306561]> ~/sopr.sh
### Set shorter prompt to better fit when pasted in StackOverflow (or other) pages ###

[064bit prompt]> ls
code00.c
[064bit prompt]> gcc -o cwd code00.c
[064bit prompt]>
[064bit prompt]> ls
code00.c  cwd
[064bit prompt]> ./cwd
CWD: [/mnt/e/Work/Dev/StackOverflow/q054306561]
[064bit prompt]>
[064bit prompt]> pushd .. && ./q054306561/cwd && popd
/mnt/e/Work/Dev/StackOverflow /mnt/e/Work/Dev/StackOverflow/q054306561
CWD: [/mnt/e/Work/Dev/StackOverflow]
/mnt/e/Work/Dev/StackOverflow/q054306561
[064bit prompt]>
[064bit prompt]> mkdir test && pushd test && ../cwd && popd
/mnt/e/Work/Dev/StackOverflow/q054306561/test /mnt/e/Work/Dev/StackOverflow/q054306561
CWD: [/mnt/e/Work/Dev/StackOverflow/q054306561/test]
/mnt/e/Work/Dev/StackOverflow/q054306561

相关问题