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

时间:2012-11-18 03:59:51

标签: c file-io file-copying

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

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

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

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

我到目前为止:

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

2 个答案:

答案 0 :(得分:1)

在文件名前添加您希望放置的目录。或者,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

答案 1 :(得分:1)

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

其中backup包含“folder \ filename”,其中文件夹是你所在的文件夹

相关问题