Creating a folder (FULL file path)

Hello everyone!

I've just started working in C++, so please, no complicated explanations.

My question is: how can you create a folder by inputting the entire file path?

I saw that mkdir can be used only like this: mkdir("c:\directory1) and not like this: mkdir("c:\directory\directory2\directory3\directory4)

The most simple explanation i could find is:

-> i write the file path (lets say, c:\program files\c++\projects) and the program should do this:
-> mkdir("c:\program files");
-> mkdir("c:\program files\c++");
-> mkdir("c:\program files\c++\projects);

The problem here is that the file path is not fixed, i mean it is what i type (it can be c:\program files\test, or d:\media\audio or x:\directory1\directory2\directory3)

Hope i made myself clear, if there is anything you dont understand, or if you need more details, PLEASE, feel free to ask.

Thank you!
You should use the "find()" method from the strings library to determine where the backslashes "\" are. Then use that to calculate the arguments for the "substr()" method, conveniently also from the strings library, and use the variable that is returned to create each subsequent sub folder in whatever way you choose. Ideally this should be a recursive operation and some beginners have trouble with that concept.

EDIT: Since you have already stated that you plan to use "mkdir" I'll assume you're doing this via the "system()" function from cstdlib. Some people might tell you that this function is OS dependent so it should be avoided, but since this entire operation is OS dependent the only proper response to that is "No S***". In actuallity since the "mkdir" command is one of the very few recognized by Unix, DOS and Windows you've accidently found a method of doing this which is technically better then resorting to the relevant API.

ANOTHER EDIT: Don't forget that the backslash is an escape character in strings so you have to double up on them.
Last edited on
Thank you very much for answering. I was afraid you'd mention about that damn substr thing, because i've used it and recieved an error (std:out of memory or something like that)

I dont really plan to use mkdir, i also tried createdirectory but with no success.

When you have some spare time, can you please write down a code?
We don't usually write code. If you want to give it a shot we can tell you what might be causing your code not to work though.
Oh ok. I'll come back tomorrow with some info about my code.

Until then, tell me, why don't you write codes?
It's just something that was established in the culture of this site before I got here. We would rather help people learn to do it then just hand them answers.
I think i fixed it... The problem is that when i declared the string i wrote "C:\Program Files\Folder", and after i wrote "C:\\Program Files\\Foler" i didnt get any error!

I don't know if it matters anymore, but here is the error:
First-chance exception at 0x76009617 in Application 4.exe: Microsoft C++ exception: std::out_of_range at memory location 0x0031f658..
Can you tell me, or at least try to explain why was this happened?

Thank you very much!
Topic archived. No new replies allowed.