首页 > 解决方案 > 我想将我当前的工作目录位置复制到一个新的文本文件中

问题描述

我想将我当前的工作目录位置复制到新的文本文件中。例如,我在“xyz”目录中工作,我想将xyz目录路径复制到abc.text文件中。

在 Centos 或任何 Linux 操作系统中是否可行?

标签: cshell

解决方案


#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(void)
{
    FILE *fp;
    char buf[PATH_MAX];

    if (getcwd(buf, sizeof(buf)) == NULL) {
        perror("getcwd");
        exit(1);
    }

    fp = fopen("abc.txt", "w");

    if (fp == NULL) {
        perror("fopen");
        exit(1);
    }

    fprintf(fp, "%s\n", buf);
    fclose(fp);
    return 0;
}

pwd > abc.txt

推荐阅读