[Solved] convert int to const char* in order to write on file


The first parameter to fputs is a char*, 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.

int a = 5;

//the C way
FILE* pFile = fopen("myfile.txt","w");
char buffer[12];
atoi(a, buffer, 10);
fputs(buffer, pFile); 
fclose (pFile);
//or
FILE* pFile = fopen("myfile.txt","w");
fprintf(pfile, "%d", a);
fclose(pfile);

//the C++ way
std::ofstream file("myfile.txt");
std::stringstream ss;
ss << a;
file << ss.str();
//or
std::ofstream file("myfile.txt");
file << a;

2

solved convert int to const char* in order to write on file