使用c程序创建目录

时间:2015-01-16 21:48:05

标签: c input directory

我正在尝试创建一个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;

}

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

//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;

}

2 个答案:

答案 0 :(得分:1)

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, '.');

答案 1 :(得分:0)

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] = '\0';
    }

    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