用c程序创建一个目录

Making a directory with a c program

我正在尝试编写一个 C 程序,它接受一个字符串并创建一个具有给定名称的目录。到目前为止,我已经制作了两个版本,它们包含在下面,但都没有像我希望的那样工作。 但是这个程序有两个问题: 1.点击回车后才开始输入 2. 使目录以问号结尾。

//Make Directory program

#include<stdio.h>
#include<string.h>



void main()
{
  char dirname[20];
  fgets(dirname, 20, stdin);
  int check;
  check = mkdir(dirname);
  printf("This is the chosen directory name: ");
  printf(dirname);

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

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

  return;

}

我也试过这个版本。 但是每当我尝试 运行 它时它就会出现段错误。 我试过输入。 "directory" 和 目录

//Make Directory program

#include<stdio.h>
#include<string.h>
void main( char dirname[20])
{
  int check;
  checker = mkdir(dirname);

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

 else
 {
   printf("Unable to make directory\n");
 }
  return;
}

如有任何帮助,我们将不胜感激


编辑: 这是根据以下建议编辑的新代码

当我输入时: $ makedir 目录

它创建了一个名为: p????

非常感谢您到目前为止的帮助。

//Make Directory program

#include<stdio.h>
#include<string.h>



void main(int argc, char *argv[])
{
 // char dirname[20];
//  fgets(dirname, 20, stdin);
  int check;
  check = mkdir(argv, '.');
  //mkdir(argv, '.');

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

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

  return;

}

mkdir 将 const char * 作为参数而不是指针数组。

int mkdir(const char *pathname, mode_t mode);

如手册页所述

http://man7.org/linux/man-pages/man2/mkdir.2.html

尝试:

  int check;
  //the index of your parameter
  check = mkdir(argv[1], 0755);
  //mkdir(argv, '.');
the following code is from your first code posted
comments are added to indicate what was wrong with the code

// suggest reading/understanding the man pages for the system functions
// used in your coding, before actually using the functions

//Make Directory program

// place spaces between include and <, for readability
#include <stdio.h>
#include <stdlib.h>    // needed for exit() and EXIT_FAILURE
#include <string.h>    // needed for strlen()
#include <sys/stat.h>  // needed by mkdir()
#include <sys/types.h> // needed by mkdir()

#define MAX_DIRNAME_LEN (20)

// main always returns an int, not a void
int main()
{
    char dirname[MAX_DIRNAME_LEN];  // this seems rather short for directory name buffer

    // need to output a prompt so user knows what to do
    printf( "\n please enter a directory name, max 18 characters:");
    // 18 characters allows for the newline and the nul termination byte
    // on windows/DOS it would be 17 characters
    //   as a newline on windows/DOS is 2 characters

    // need to check for errors
    if( NULL == fgets(dirname, sizeof(dirname), stdin) )
    { // then, fgets failed
        perror( "fgets for directory name failed" );
        exit( EXIT_FAILURE );
    }

    // implied else, fgets successful

    // fgets() inputs the newline, so need to remove it
    // need to remove the '\n' from the end of the directory name:
    if( (dirname[strlen(dirname)]-1) == '\n') // note: this check will not work correctly on windows/DOS
                                              //       because their newline is 2 characters
    {
        dirname[strlen(dirname)-1] = '[=10=]';
    }

    int check;

    // here is the prototype for mkdir:
    // int mkdir(const char *pathname, mode_t mode);
    //
    // as you can see, that is not what your code is doing.
    // if your makefile (compile/link steps) has enabled all the warings
    // (for gcc, that would be -Wall -Wextra -pedantic)
    // then the compiler would have warned you about the problem


    if( 0 !=  (check = mkdir(dirname, 01666) ) ) // returns 0 on success
    { // then  mkdir failed
        perror( "mkdir failed" );
        printf("Unable to create directory\n");
        exit( EXIT_FAILURE );
    }

    // implied else, mkdir successful

    printf("This is the chosen directory name: \n%s\n Directory Created\n", dirname);

    // main returns an int, 0 is seen as success
    return(0);
} // end function: main