1 Replies - 9556 Views - Last Post: 11 November 2012 - 12:01 PM Rate Topic: -----

#1 chipicau   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 48
  • Joined: 15-May 12

Append char to wchar

Posted 11 November 2012 - 08:19 AM

Hello.

I'm trying to make a program that prints a random file path. Here's the code I have, but I don't think it's correct because it crashes.
This code is supposed to create a new wchar variable, fill it with the system directory path, add "\x", and add a char converted from a random int between 97-123.

	srand(time(NULL));
	WCHAR path[MAX_PATH];
	wcscpy_s(path, systemdirectory);
	wcscat_s(path, L"\\x");
	WCHAR randomname[MAX_PATH];
	swprintf_s(randomname, L"%s", char(rand() % (123 - 97) + 97)); //this crashes my program
	wcscat_s(path, randomname);
	wcscat_s(path, L".exe");

	std::wcout << path;



The error is: "Access violation reading location 0x0000006a."

Any hints?
Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: Append char to wchar

#2 chipicau   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 48
  • Joined: 15-May 12

Re: Append char to wchar

Posted 11 November 2012 - 12:01 PM

Well, I ended up setting Character Set property in VC++ to Not Set (Configuration Properties - General - Project Defaults - Character Set), which enabled me to use CHAR instead of WCHAR for Win32 API functions.

Then I used a std::string like this to set up the random path:

srand(time(NULL));

std::string path;
path += systemdirectory;
path += "\\x";
path += rand() % (123 - 97) + 97;
path += rand() % (123 - 97) + 97;
path += rand() % (123 - 97) + 97;
path += rand() % (123 - 97) + 97;
path += rand() % (123 - 97) + 97;
path += rand() % (123 - 97) + 97;
path += rand() % (123 - 97) + 97;
path += rand() % (123 - 97) + 97;
path += rand() % (123 - 97) + 97;
path += ".exe";
std::cout << path;



Thanks anyway. Furthermore, I have another problem that whoever is reading this could check in.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1