繁体   English   中英

使用AC程序建立目录

[英]Making a directory with a c program

我正在尝试制作一个C程序,该程序需要一个字符串,并使用给定名称创建目录。 到目前为止,我已经制作了两个版本,它们包含在下面,但都无法像我希望的那样工作。 但是此程序有两个问题:1.直到您单击Enter后,它才需要输入。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;

}

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM