Create new file with cstdio::tmpnam() help!

I am making a simple program that creates a file with a random name with cstdio::tmpnam() in order not to overwrite previously created files.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <fstream>
#include <cstdio>

using namespace std;

int main() {

	ofstream fout;
	char* tname = tmpnam(0);

	fout.open(tname);
	fout << "Content of file with random name." << endl;
	fout.close();

	system("pause");
	return 0;
}


For some reason it does not create the file. Why? How do I solve it but still use cstdio::tmpnam() ? Can tmpnam() only be used for temporary files?
Last edited on
I realized that the tmpnam() function returns a name that starts with a '\' in the first element. Any way to use this function or another similar function that does not return a name with the '\' in the first element?
Last edited on
Topic archived. No new replies allowed.