简体   繁体   English

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

[英]How do I create a file inside a hidden directory?

Really, I'm just generally confused on how to create a file in a subdirectory of the current working directory, but especially if it's hidden. 真的,我只是对如何在当前工作目录的子目录中创建文件感到困惑,但特别是如果它被隐藏了。

Say char* backup contains the name of the file we're creating with an open() call. 假设char* backup包含我们使用open()调用创建的文件的名称。

Say there's a hidden folder which already exists called .mybackup . 假设有一个已存在的隐藏文件夹,名为.mybackup

How do I create the file inside of .mybackup ? 如何在.mybackup创建文件? Hopefully without chdir'ing twice. 希望没有两次chdir'ing。

Where I'm at so far: 到目前为止我在哪里:

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

Prepend the filename with the directory you'd like it to be placed in. Or, chdir ing twice would also be acceptable. 在文件名前加上您希望放置的目录。或者, 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
int filewrite;
filewrite = open(backup, O_CREAT | O_RDWR, 0644)

where backup contains "folder\\filename" where folder is relative from the folder you are in 其中backup包含“folder \\ filename”,其中folder与所在的文件夹相对

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM