hi guys.

i want a help on making directories in c++.i have used the code to declare and intilliaze the path name and folder name..but the probelem is that i want it to to ask from the user for the drive and folder name..i tried but i didnt get the solution to it ..plz help

m using borland c++ 4.5

for example :
enter the direcorie path and folder name: d:\\abc

but when i do this a pointer error comes there..

#include<direct.h>
#include<iostream.h>
#include<stdio.h>

void makedir(char *n)
{
mkdir(n);
}

int main()
{
char *dir;

cout<<"enter teh directorie you want to create ";
gets(dir);
makedir(dir);
return 0;
}

Recommended Answers

All 2 Replies

dir is an uninitialized pointer, it's not pointing to any memory, so when you use gets (which you shouldn't regardless) you can enter characters until the cows come home and run right over whatever memory is adjacent to it.

I would set up dir as a char array holding 256 characters and use fgets to read in the directory name. fgets can leave the '\n' in the string, so look for it and replace it with a null terminus '\0'

Your compiler is a 16-bit compiler that only runs under MS-DOS and knows nothing about long file names, such as names with spaces. It will be impossible that compiler to create a new directory that contains spaces like "Program Files" or names longer than 8 characters and 3 character extension.

Check your spelling -- if you are going to display something on the screen it should at least be spelled correctly.

commented: Good point. I had thought that about the prompt also. +6
commented: Good and detail solution +1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.