This program should create 10 randomly named files with unicode string names on a desktop but it only creates just 1. I tried using delete[] statements at the end of the createfiles
function to deallocate the memory for output, file and desktop but it still doesn't work. Am I doing something wrong?
#include "shlobj_core.h"
#include <fstream>
#include <iostream>
#include <Windows.h>
wchar_t* generateRandomUnicodeString(size_t len, size_t start, size_t end)
{
wchar_t* ustr = new wchar_t[len + 1];
size_t intervalLength = end - start + 1;
srand(time(NULL));
for (auto i = 0; i < len; i++) {
ustr[i] = (rand() % intervalLength) + start;
}
ustr[len] = L'\0';
return ustr;
}
void createfiles(int number)
{
if (number == 0) {
return;
}
wchar_t* output = generateRandomUnicodeString(5, 0x0400, 0x04FF);
LPWSTR desktop;
LPWSTR file;
SHGetKnownFolderPath(FOLDERID_Desktop, 0, 0, &desktop);
PathAllocCombine(desktop, output, 0, &file);
std::wofstream ofs(file);
ofs << "File Description";
delete[] output;
output = nullptr;
delete[] file;
file = nullptr;
delete[] desktop;
desktop = nullptr;
createfiles(number - 1);
}
int main()
{
createfiles(10);
return 0;
}