Using loop to create numbered folders (windows)

Hi,

I'm trying to create a set of folders using a for loop. I am running into problems. I've tried searching (I would have assumed this would be a common thing people would want to do) but no luck. I have created a minimal segment of code that reproduces the error. This particular segment should produce folders

C:\1.00
C:\2.00
C:\3.00
.
.
.
C:\10.00

The code is

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <direct.h>
#include <windows.h>
#include <atlstr.h>
#include <iomanip>
#include <sstream>
#include <string>
using namespace std;
void main()
{
	int t;
	for(t=1;t<11;t++)
	{
		ostringstream oss;
oss << fixed << showpoint << setprecision(3) << t;
string t_string = oss.str();

	string output_dir = "C:\\";
	
	_mkdir(output_dir+t_string);
	}
}


I couldn't remember which files in the header were needed for these specific tasks, so I just left all of the ones in my full code in.

The error I get is

'_mkdir' : cannot convert parameter 1 from 'std::basic_string<_Elem,_Traits,_Ax>; to 'const char *

If I change the

_mkdir(output_dir+t_string);

line to

CreateDirectory(output_dir+t_string,NULL);

I get the error

'CreateDirectoryW' : cannot convert parameter 1 from 'std::basic+string<_Elem,_Traits,_Ax>' to 'LPCWSTR'

I'm happy to use a windows specific solution. I'd probably prefer not to use any 3rd party software. Hopefully someone can help me work out a solution.
try _mkdir( (output_dir+t_string).c_str() );
Last edited on
That worked perfectly, thanks :)

Strangely there is an issue with my test code not showing the decimal places, but it's working fine in my real code.
Topic archived. No new replies allowed.