【问题标题】:How to create file inside a directory using C如何使用C在目录中创建文件
【发布时间】:2014-05-21 21:14:18
【问题描述】:

我正在尝试在目录中创建一个目录和一个文件。下面是我的 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);
}

感谢帮助

【问题讨论】:

  • 你的 / 放错了 open(directory"/my_log.txt", O_RDWR | O_APPEND | O_CREAT);
  • 你的意思是这样的:int filedescriptor = open(directory"/my_log.txt", O_RDWR | O_APPEND | O_CREAT);

标签: c file directory


【解决方案1】:

使用 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”。
  • @Tony - 在创建文件之前,您是否正在检查该目录是否存在?请参阅我对答案的编辑以了解我的意思。
【解决方案2】:

你应该这样做:

char *filepath = malloc(strlen(directory) + strlen("my_log.txt") + 2);
filepath = strcpy(filepath, directory);
filepath = strcat(filepath, "/my_log.txt");

然后在open函数中使用filepath

【讨论】:

    【解决方案3】:

    请参考完整解决方案:

    #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. 第一个参数:目录名称
    2. 第二个参数:文件名

    例如:编译后 ./executableName yourFolderName yourFileName

    【讨论】:

      猜你喜欢
      • 2016-12-21
      • 1970-01-01
      • 1970-01-01
      • 2020-11-28
      • 2018-03-18
      • 1970-01-01
      • 2012-02-14
      • 2014-08-27
      • 2012-06-20
      相关资源
      最近更新 更多