Reputation: 3802
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;
}
Upvotes: 0
Views: 1603
Reputation: 16540
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
Upvotes: 0
Reputation: 2010
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, '.');
Upvotes: 1