1

I'm trying to make a program which will create a sum of directories the user wants to create.

this is my code:

#include <cstdlib>
#include <iostream>
#include<windows.h>

using namespace std;

int main(int argc, char *argv[])    {

    int nrDirs = 0;
    cin>> nrDirs;

    for (int i = 0; i <= nrDirs; i++) {
             CreateDirectory ("C:\\Users\\myName\\Desktop\\new", NULL);
    }
    system("PAUSE");
    return EXIT_SUCCESS;
}

Now my problem, I don't know how to rename the directory. I know how to do this in Objective-C:

"C:\\Users\\myName\\Desktop\\new%i", i

But this don't work in c++. :(

So how do i do this?

sarnold
  • 102,305
  • 22
  • 181
  • 238
Arbitur
  • 38,684
  • 22
  • 91
  • 128

1 Answers1

3

Use can use CString::Format:

dirName.Format("C:\\Users\\myName\\Desktop\\new%i", i);

Use can use std::stringstream:

dirName << "C:\\Users\\myName\\Desktop\\new" << i;

Use can use sprintf:

sprintf(dirName, "C:\\Users\\myName\\Desktop\\new%i", i);

For all the above cases, dirName is a buffer that you will need to pass to CreateDirectory.

If intermediate directories in the path do not exist, use SHCreateDirectory. This API also creates intermediate directories in the path if they do not exist.

Superman
  • 3,027
  • 1
  • 15
  • 10
  • For the 3 examples it would be as follows - 1) CString dirName; 2) std::stringstream dirName; 3) char dirName[MAX_PATH]; – Superman Jun 21 '12 at 05:10