如何在C中创建文件夹

时间:2018-02-06 17:38:46

标签: c email directory

如何为电子邮件管理员创建新文件夹,我有这段代码,但它没有用。

 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();
 }

3 个答案:

答案 0 :(得分:0)

使用此:

 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数组。

答案 1 :(得分:0)

您必须为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);

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

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

答案 2 :(得分:0)

我从Mahonri Moriancumer's anwer复制:

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