如何使用C语言在目录中创建文件

3

我试图创建一个文件夹和里面的文件。以下是我的C代码,但是当我尝试编译它时,我得到了这个错误:invalid operands to binary / (have ‘const char *’ and ‘char *’)

char *directory = "my_dir";

struct stat dir = {0};
if(stat(directory, &dir) == -1)
{
    mkdir(directory, 0755);
    printf("created directory testdir successfully! \n");
}

int filedescriptor = open(directory/"my_log.txt", O_RDWR | O_APPEND | O_CREAT);
if (filedescriptor < 0)
{
    perror("Error creating my_log file\n");
    exit(-1);
}

感谢帮助。

1
你的 / 放置得不好 open(directory"/my_log.txt", O_RDWR | O_APPEND | O_CREAT); - Alexis
你的意思是像这样吗:int filedescriptor = open(directory"/my_log.txt", O_RDWR | O_APPEND | O_CREAT); - TonyGW
3个回答

7
使用sprintf()或类似函数创建pathFilename字符串:
char pathFile[MAX_PATHNAME_LEN];
sprintf(pathFile, "%s\\my_log.txt", directory );

然后
int filedescriptor = open(pathFile, O_RDWR | O_APPEND | O_CREAT);   

注意:如果您正在使用Linux,请将\\更改为/,并将MAX_PATHNAME_LEN更改为260(或任何Linux喜欢用于该值的值)。

编辑:如果您需要在创建文件之前检查目录是否存在,可以执行以下操作:

if (stat("/dir1/my_dir", &st) == -1) {
    mkdir("/dir1/my_dir", 0700);
}   

更多信息请参考:statmkdir


谢谢,但是这会创建一个名为“my_dir\my_log.txt”的文件,文件my_log.txt不在test_dir文件夹内。它也不会创建“my_dir”文件夹。 - TonyGW
@Tony - 在创建文件之前,你是否检查目录是否存在?请看我对答案的编辑,了解我的意思。 - ryyker

1
你应该做一些类似这样的事情:
char *filepath = malloc(strlen(directory) + strlen("my_log.txt") + 2);
filepath = strcpy(filepath, directory);
filepath = strcat(filepath, "/my_log.txt");

然后在open函数中使用文件路径。

0
请参考完整解决方案:
#include<stdio.h>
#include<string.h>  // for string operations
#include<stdlib.h>  //for malloc()
#include<fcntl.h>   //for creat()
#include<sys/stat.h>    //for struct stat, stat()
#include<unistd.h>  //for close()

int main(int argc,const char **argv)
{
    //variable declaration
    int iFd = 0;
    char *chDirName = NULL;
    char *chFileName = NULL;
    char *chFullPath = NULL;
    struct stat sfileInfo;

    //Argument Validation
    if(argc != 3)
    {
        printf("[ERROR] Insufficient Arguments\n");
        return(-1);
    }

    //argument processing
    chDirName = (char *)malloc(sizeof(char));
    chFileName = (char *)malloc(sizeof(char));
    chFullPath = (char *)malloc(sizeof(char));
    chDirName = strcpy(chDirName,argv[1]);
    chFileName = strcpy(chFileName,argv[2]);

    //create full path of file
    sprintf(chFullPath,"%s/%s",chDirName,chFileName);

    //check directory exists or not
    if(stat(chDirName,&sfileInfo) == -1)
    {
        mkdir(chDirName,0700);
        printf("[INFO] Directory Created: %s\n",chDirName);
    }

    //create file inside given directory
    iFd = creat(chFullPath,0644);

    if(iFd == -1)
    {
        printf("[ERROR] Unable to create file: %s\n",chFullPath);
        free(chDirName);
        free(chFileName);
        free(chFullPath);
        return(-1);
    }

    printf("[INFO] File Created Successfully : %s\n",chFullPath);

    //close resources
    close(iFd);
    free(chDirName);
    free(chFileName);
    free(chFullPath);

    return(0);
}

使用两个命令行参数运行程序:

  1. 第一个参数:DirectoryName(目录名称)
  2. 第二个参数:FileName(文件名称)

例如:编译后执行以下命令:./executableName yourFolderName yourFileName


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接