cmd mkdir in c++ system();

why the production of the useless program I did is weird *_*
it produce 10 ABCDEFG folders in different orders then after it finishes it produced folders with names like:
_concurrence_unlock_error
_globals
M_replace_aux

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;

int main()
{

    int counter = 0;
    while(1)
    {
            string stringdir = " ABCDEF "+counter ;
    system(("mkdir "+stringdir).c_str() );
    counter++;

    }

}


I thought of making the console working but hidden , but that will be illicit :D
Last edited on
The string literal will decay to a pointer (const char*) pointing to the first character in the string literal " ABCDEF ". When you add the counter you get a pointer that points that many bytes ahead. So when counter is 1 it will point to the A (and will be treated as the string "ABCDEF "), when counter is 2 it will point to the B (and will be treated as the string "BCDEF "), etc.

When you go beyond the string length (counter > 9) it will point to whatever is stored after the string literal in memory. Most (all?) string literals that are used in the program are probably stored after each other in memory so that is probably why you get all those other weird names.
Last edited on
I get it, thanks.
As Peter87 has alread said, part of the problem is that the string literal (" ABCDEF ") is treated as a pointer and operator+ (with the counter) is performing pointer arithmetic. (And thereby finding all kinds of weird names in memory to use.)

Another part is there is no automatic conversion of integers to string, so this will also fail (to use the same tricks as is used on line 13 of the original post.)

string stringdir = string(" ABCDEF ") + counter ; // Fails to compile!

You can, however use an ostringstream (from <sstream> header) which does know how to deal with integers (when you use operator<<).

1
2
3
ostringstream oss;
oss << " ABCDEF " << counter ;
// oss.str() returns the string object 


Finally, there's no need to use the system() function to run the mkdir command when you can use the corresponding function:

POSIX

int mkdir(const char *path, mode_t mode);

mkdir
http://pubs.opengroup.org/onlinepubs/009695399/functions/mkdir.html
from header <sys/stat.h>

Microsoft CRT

1
2
3
int _mkdir(
   const char *dirname 
);


_mkdir, _wmkdir
http://msdn.microsoft.com/en-us/library/2fkk4dzw%28v=vs.80%29.aspx
from header <direct.h>

Which leads to (using the Microsoft CRT)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <sstream>
#include <string>
#include <direct.h> // for _mkdir
using namespace std;

int main()
{
    int counter = 0;
    while(counter < 10)
    {
        ostringstream oss;
        oss << "ABCDEF " << counter;
        _mkdir(oss.str().c_str());
        counter++;
    }
    return 0; // main returns 0 even if it's not explicit,
              // so you might as well make this clear!
}


Andy
Last edited on
Sweet I just created a folder with hundreds of thousands of folders ^_^
and with the <direct.h> header I don't have to use shell to use mkdir function , just using microsoft run time library
Topic archived. No new replies allowed.