I have the following code in C++ and I would like to convert a integer to a const char* in order to write on a file. I tried itoa or sstream functions but it's not working.
FILE * pFile;
pFile = fopen ("myfile.txt","w");
int a = 5;
fputs (&a,pFile);
fclose (pFile);
Thanks in advance
The first parameter to
fputs
is achar*
, so the code you show is obviously incorrect.You say
I tried itoa or sstream functions but it's not working.
but those are the solutions, and there's no reason for them not to work.Try
itoa(a)
it converts an i nt to a rray henceitoa
What's wrong with
fprintf
? Orsnprintf
and thenfputs
the result.Use type-casting. You can use boost::lexical_cast
Once in a string, you can use the c_str() member function to get a const char *