如何在C中创建文件夹

neekobn8  于 2022-12-29  发布在  其他
关注(0)|答案(3)|浏览(223)

我怎么才能创建一个新的文件夹的电子邮件管理器,我有这个代码,但它不工作.

void create_folder() {
   int check;
   char * dirname;
   clrscr();
   printf("Enter a directory path and name to create a folder (C:/name):");
   gets(dirname);
   check = mkdir(dirname);

   if (!check)
     printf("Folder created\n");

   else {
     printf("Unable to create folder\n");
     exit(1);
   }
   getch();
   system("dir/p");
   getch();
 }
h22fl7wq

h22fl7wq1#

请使用此选项:

void create_folder() {
   int check;
   char dirname[128];
   clrscr();
   printf("Enter a directory path and name to create a folder (C:/name):");
   fgets(dirname, sizeof(dirname), stdin);
   check = mkdir(dirname);

   if (!check)
     printf("Folder created\n");

   else {
     printf("Unable to create folder\n");
     exit(1);
   }
   getch();
   system("dir/p");
   getch();
 }

您的dirname字符串未分配,请使用char数组。

z0qdvdin

z0qdvdin2#

我从Mahonri Moriancumer's anwer复制:

void make_directory(const char* name) {
   #ifdef __linux__
       mkdir(name, 777); 
   #else
       _mkdir(name);
   #endif
}
5w9g7ksd

5w9g7ksd3#

您必须为dirname分配内存。

#include<stdio.h>
#include<conio.h>
#include<process.h>
#include<stdlib.h>
#include<dir.h>

#define SIZE 25      //Maximum Length of name of folder

void main() {
    int check;
    char *dirname;
    dirname=malloc(SIZE*sizeof(char));
    printf("Enter a directory path and name to be created (C:/name):");
    gets(dirname);
    check = mkdir(dirname);
    free(dirname);
    if (!check)
        printf("Directory created\n");

    else
    {
        printf("Unable to create directory\n");
        exit(1);  
    } 
    getch();
    system("dir/p");
    getch();
    }

相关问题