Saving text file using C++ and WinAPI

  • Thread starter Thread starter Zuhlik
  • Start date Start date
Z

Zuhlik

Guest
#include <iostream>
#include <windows.h>
#include <string>
#include <time.h>

#define MIN 1
#define MAX 100

using namespace std;

int random(int min, int max);

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

HANDLE hFile;
DWORD bytesWritten = 0;

for (int i=0; i<100; i++){
numbers = (random(MIN, MAX));
cout << numbers << " ";
}

hFile = CreateFile("random.txt", GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
WriteFile(hFile, numbers, sizeof(numbers), &bytesWritten, NULL);

CloseHandle(hFile);

return 0;
}

int random(int min, int max)
{
static bool first = true;
if (first)
{
srand( time(NULL) );
first = false;
}
return min + rand() % (( max + 1 ) - min);
}

I'm trying to save random numbers to text file using WinAPI and pure C++ using the above code. The file is saved, but the numbers are illegible. What is the simplest and proper way to do that in C++/WinAPI? I tried to convert int to string using sprintf() and to_string() functions but without success.

Continue reading...
 
Back
Top