c create folder
September 10, 2010 11:47 AM   Subscribe

How can I create a folder on my desktop in c or c++ code?

I'm using Visual Studio 2008 (c++) and for some reason I can't figure out how to make it work. I've tried a few different things. It's a Win32 Console Application (Empty Project---no precompiled header). I add a .cpp file, and write the code there. Here's the simplest I've tried:

#include
#include
#include
#include

using namespace std;

int main()
{
CreateDirectory("c:\\dirn.txt");
}

It gives this error:

error C2660: 'CreateDirectoryW' : function does not take 1 arguments
posted by amsterdam63 to Computers & Internet (14 answers total) 2 users marked this as a favorite
 
Looks like you need to pass in more arguments - a quick google search gave me this
CreateDirectory("C:\\thing.txt",NULL);
posted by Tomorrowful at 11:53 AM on September 10, 2010


Dammit, HTML goof. That should be, this page, which indicates that you need a second argument representing security attributes - or a null for defaults. Example: CreateDirectory("C:\\thing.txt",NULL);
posted by Tomorrowful at 11:54 AM on September 10, 2010


Just as the error message says, CreateDirectory requires two arguments. You might be confused because the second one is "optional," but that means you can use NULL instead of omitting it.
posted by grouse at 11:55 AM on September 10, 2010


If you want a single-argument function there's _mkdir() which is from the C Runtime Library instead of the Win32 API. Of course under the hood _mkdir() calls CreateDirectory() so it's really more of a compatibility thing.
posted by Rhomboid at 12:19 PM on September 10, 2010


Response by poster: If I use this as my code:

#include
#include
#include
#include

using namespace std;

int main()
{


CreateDirectory("c:\\dirn.txt", NULL);
}

I get:

error C2664: 'CreateDirectoryW' : cannot convert parameter 1 from 'const char [12]' to 'LPCWSTR'

At which point I went to this website "google" Biru just told me about, and did a little research. This seems to do the best job of explaining what the problem might be, but I'm too big of a moron to figure out how it could help me:

Any thoughts?

posted by amsterdam63 at 12:27 PM on September 10, 2010


Try _T("C:\\dirn.txt") instead.

This stuff is not necessarily easy to pickup piecewise. I recommend getting a book. Unfortunately I cannot suggest a good one, but maybe someone else can.
posted by grouse at 12:31 PM on September 10, 2010


This stuff is not necessarily easy to pickup piecewise.

I would say that it's impossible to pickup piecewise. OP: if you really want to get good at this, you need to first pick up a good book on C/C++. I don't have any suggestions -- it's been 15 years since I first started this -- but usually when I learn a new language I just camp out at the Borders for an hour. Read and flip through a couple books, until you find one that makes sense to you.

After you learn C/C++, then you need to learn the Win32API. Windows API programming is a whole different beast. You might not even find many books on it anymore.

If I were starting out today, I'd honestly skip the raw C/C++ stuff for Windows. The future is in .Net and C#. Unless you have a specific project in mind, it's not worth the effort to learn C.

Now, if your ultimate goal were Objective-C/iPhone programming, you would need to have a strong background in C. But not Windows.
posted by sbutler at 12:50 PM on September 10, 2010


In your project settings somewhere is an option for Unicode or ANSI. If you select Unicode, which is the default, it means all the Win32 API calls map to the 'W' version instead of the 'A' version, and it means you're using wide strings in your code. To declare a wide string constant you have to use L"string". The _T() macro exists so that you can write code that can compile both ways, with Unicode enabled or not. It expands to the L"string" version if Unicode is enabled, otherwise it evaluates to nothing and you get plain char [] string constants. That's what the error message is telling you -- LPCWSTR stands for long pointer to constant wide string.
posted by Rhomboid at 12:50 PM on September 10, 2010


Response by poster: Ok, _mkdir works fine. Thanks for the help.

And I appreciate all the advice. I'm not looking to become any sort of professional programmer, I'm just working on a few small things, so it's frustrating when a seemingly simple task becomes like a wild goose chase. It's nice when someone takes a little time to help me figure out something that I assumed would be a little more simple. (and I guess turns out was.)
posted by amsterdam63 at 1:07 PM on September 10, 2010


If you are just working on a few things and you have Visual Studio, you really should try C#. There is still lots of places for you to get confused but these kinds of details are almost entirely abstracted away from you. Even better would be to use something like IronPython so that you can use a simpler language like Python to interact with all the .Net classes.
posted by mmascolino at 1:20 PM on September 10, 2010


LPCWSTR would imply wide characters/unicode. I don't really do Windows, but you might try something like:

CreateDir(L"C:\\blah", NULL);

Also, as a novice, you probably don't want to be doing things in C/C++. You'll just keep hitting you head on brick walls.
posted by chairface at 1:31 PM on September 10, 2010


If you really want to just do a few small tasks, I would strongly suggest that C++ and Win32 is not the way to do it. If it seems nuts that this was so difficult, that's because it is somewhat nuts. If you continue down this path you will be bashing your head against the wall trying to figure out why every somewhat simple task is so arbitrarily difficult. It's not going to get easier.

I recommend Python as well (although I would probably use the regular CPython rather than IronPython), but C# would probably be fine.

It's nice when someone takes a little time to help me figure out something that I assumed would be a little more simple.

We are trying to save you a lot of time and frustration in the future.
posted by grouse at 1:31 PM on September 10, 2010 [1 favorite]


Note also that you aren't going to get squat to appear on the desktop by creating a folder under the C drive. You are going to have to create it under your desktop folder and this, while not particularly hard, is going to have you scratching your head even more.

You have to look up the location of your desktop by using the function SHGetSpecialFolderPath, unless you are on Windows 2000, NT, or XP, in which case it's SHGetFolderPath. If you are on Vista or Windows 7, of course, you should use SHGetKnownFolderPath. And then shoot yourself. In the next version of Windows it will be some other damn thing. Simple, really.

Nthing the advice to use anything other than C or C++. Seriously. Pick a language at random and you'll be happier. I note that if I google for "Python" and "desktop location" I immediately get a code snippet that can be used to find your desktop folder, so that's good.
posted by It's Never Lurgi at 3:58 PM on September 10, 2010 [1 favorite]


And just to pile on, if this is meant to be something for distribution to other computers, you may run into security issues. I bet Vista and Win7 don't care for some program writing shit to the desktop without the user's intervention.

And other possible FUBARs with spaces in names and the like. I shudder at the idea of messing around in the filesystem with a one-off executable. One logic error in a loop and you've got a couple thousand directories on someone's desktop.
posted by gjc at 4:51 PM on September 10, 2010


« Older Help can't suddenly grow strings, nor does it buy...   |   Slow-mo voice driving me crazy Newer »
This thread is closed to new comments.