0

I am trying to make a C program which takes a string and makes a directory with the given name. I have made two versions thus far and they are included below but neither work like I want them to. But this program has 2 problems: 1. It doesn't take input until after you click enter 2. It makes the directory end with a question mark.

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

}

I also tried this version. But it segfaults whenever I try to run it. I have tried the inputs. "directory" and 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;
}

Any help would be greatly appreciated


Edit: Here is the new code edited given the suggestions below

When I enter: $ makedir directory

it makes a directory named: p?????

Thank you very much for your help so far.

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

}
gauteh
  • 14,017
  • 3
  • 25
  • 32
Rorschach
  • 3,104
  • 3
  • 24
  • 60
  • 1
    in the first case what is the input? and in the second what do you think this is `void main( char dirname[20])`?, also it doesn't take input until you `click`? you mean press! enter, sure, `fgets()` reads the number of requested characters or until a `'\n'` is found in the input. – Iharob Al Asimi Jan 16 '15 at 21:49
  • 1
    You do not have [valid `main` declarations](http://stackoverflow.com/q/2108192/10077). – Fred Larson Jan 16 '15 at 21:52
  • The input I have been using for the first one is just: directory – Rorschach Jan 16 '15 at 21:52
  • Note that [`fgets` will include the newline character in the buffer](http://en.cppreference.com/w/c/io/fgets). That might get changed to a question mark either when creating the directory or listing it. Not sure, though. – Fred Larson Jan 16 '15 at 21:55
  • 2
    You may want to check your system man page on mkdir(2). It typically accepts two parameters. – user590028 Jan 16 '15 at 21:55
  • Thanks Fred that has helped. That was definitely at least one of my problems. I'll update in a second. – Rorschach Jan 16 '15 at 21:58
  • Right now it makes the directory but the naming is really strange. Anyone know how to fix this? I have edited the question and added another piece of code. – Rorschach Jan 16 '15 at 22:06
  • What OS are you using? – Fred Larson Jan 16 '15 at 22:16
  • see [mkdir](http://manpages.ubuntu.com/manpages/utopic/en/man2/mkdir.2.html) or [_mkdir](http://msdn.microsoft.com/en-us/library/2fkk4dzw.aspx) – BLUEPIXY Jan 16 '15 at 22:17
  • i am ssh-ing into what I believe is probably redhat (using putty) – Rorschach Jan 16 '15 at 22:23
  • I now realize that *argv is just the name of the program so I am working from there right now. – Rorschach Jan 16 '15 at 22:24

2 Answers2

1

mkdir takes const char * as an argument not a array of pointers.

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

as described in the manual page

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

try:

  int check;
  //the index of your parameter
  check = mkdir(argv[1], 0755);
  //mkdir(argv, '.');
cmidi
  • 1,590
  • 1
  • 15
  • 31
  • 2
    I don't think `'.'` is the desired mode either. Something like `0644` would be more reasonable. – Fred Larson Jan 16 '15 at 22:23
  • 1
    @FredLarson yes, sorry, mode is used to specify the permissions. From the manual : **the permissions of the created directory are (mode & ~umask & 0777).** – cmidi Jan 16 '15 at 22:33
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
user3629249
  • 15,593
  • 1
  • 16
  • 17