gpt4 book ai didi

c - 如何在隐藏目录中创建文件?

转载 作者:行者123 更新时间:2023-11-30 14:22:50 25 4
gpt4 key购买 nike

真的,我只是对如何在当前工作目录的子目录中创建文件感到困惑,特别是如果它是隐藏的。

假设 char* backup 包含我们通过 open() 调用创建的文件的名称。

假设已经存在一个名为 .mybackup 的隐藏文件夹。

如何在 .mybackup 中创建文件?希望没有 chdir'ing 两次。

到目前为止我所处的位置:

int filewrite;
filewrite = open(backup, O_CREAT | O_RDWR, 0644);

最佳答案

在文件名前面加上您想要放置的目录。或者,chdir两次也可以接受。

char* backup = "backup.sql";   // Assuming this comes from somewhere else (not constant)

const char* targetDir = ".mybackup/"; // (No leading slash)

// Allocate a buffer for the filename (remembering +1 for null-terminator!)
char* path = (char*)malloc(strlen(targetDir) + strlen(backup) + 1);

strcpy(path, targetDir); // Copy in the target directory part
strcat(path, backup); // Copy in the filename part


filewrite = open(path, O_CREAT | O_RDWR, 0644); // Open the file

free(path); // Free the buffer

关于c - 如何在隐藏目录中创建文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13437349/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com