simple string concat

Hi, I want to put a clock timer into a filename of a file I'm opening, but I can't figure out how. Something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
	//get clock time for filename
	clock_t currT = clock() * CLK_TCK;
	double elapT = currT;

	char * elapS = new char [50];itoa((int)elapT,elapS,10);

	char fileName;
	strcpy(fileName,"C:\\Folder\\output_");
	strcat(fileName,elapS);
	strcat(fileName,".txt");


	myfile.open (fileName, std::ios::app);


I get: error C2664: 'strcpy' : cannot convert parameter 1 from 'char' to 'char *'

I've tried other ways, but am still clueless.

Thanks
fileName is just a single character, and the functions you are using want a C-style string (pointer to character).

If you don't mind using C++, I would recommend looking into C++ strings and possibly stringstreams.

http://www.cplusplus.com/reference/string/string/
http://www.cplusplus.com/reference/iostream/stringstream/
I did try a stringstream, to no avail. If anyone has a simple example of something that could work for me, that would be much appreciated.

Basically, I need to convert a double or int into a string and concatenate it so that it is usable in myfile.open

well you are using

char filename , it should be

char * filename = new char[50];

do not furgate to delete above memory after use.
1
2
3
4
5
6
7
clock_t currT = clock() * CLK_TCK;

std::ostringstream oss; // string as stream

oss << "C:\\Folder\\output_" << currT << ".txt"; // write to string stream
std::string file_name = oss.str(); // get string out of stream
myfile.open(file_name.c_str()); // use c_str() to optain a const char* required for open() function. 
Last edited on
thanks, it worked!
Topic archived. No new replies allowed.