Skip to main content
GameDev.net gamedev.net
🔒 Locked

create folders automatically

Started by DrEvil Mar 11, 2005 at 4:21 PM 2 replies 1k views
Original Post
DrEvil
DrEvil
suppose you're doing file IO fstream f("c:\test1\test2\test3\testfile.txt"); this would fail if any of the test1,test2,test3 folders didnt exist. does anyone know of a method(or a library or something) that will automagically handle the possibility that folders may not exist and automatically create them?
hplus0603
hplus0603
There's a library function called mkdir() on most POSIX systems. You may need to parse the string and call mkdir() on successively longer sub-strings of the path, if it can only make a single directory.

However, the string you suggested uses un-quoted backslashes, which turn "\t" into tabs, so you probably wouldn't get the effect you're looking for :-)
enum Bool { True, False, FileNotFound };
DrEvil
DrEvil
Ok, after some digging I found something that does the trick. Figured I'd share it so others might benefit. Looks like it might be windows only though, which isn't a problem for me personally as my app is windows anyways.

SHCreateDirectoryEx()

This function creates a file system folder whose fully qualified path is given by pszPath. If one or more of the intermediate folders do not exist, they will be created as well. SHCreateDirectoryEx also verifies that the files will be visible.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.