c - 我想将我当前的工作目录位置复制到一个新的文本文件中
问题描述
我想将我当前的工作目录位置复制到新的文本文件中。例如,我在“xyz”目录中工作,我想将xyz
目录路径复制到abc.text
文件中。
在 Centos 或任何 Linux 操作系统中是否可行?
解决方案
#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
推荐阅读
- python - ModuleNotFoundError: No module named 'sklearn.tree.tree'
- html - 试图垂直居中一些跨度
- python - asyncio.wait 的替代方案?
- cython - Cython 编译警告不兼容类型
- ios - Appcenter iOS 安装错误“无法安装此应用,因为无法验证其完整性”
- sql-server - 如何调用表中每一行的过程并将结果写入新列?
- python - 带有重复的 Python 排列
- postgresql - 优化与 lpath 相关的查询的问题
- heroku - 将 Heroku 应用程序连接到 Ionos 域名
- python - lstm 输出的串联