I write to a text file like so:
void writeText(char* desc){
FILE * pFile;
pFile = fopen ("CycleTestInfo.txt","a+");
fputs (desc,pFile);
fclose(pFile);
}
I embed this in a for loop and I want to write to the file the loop that I am on:
for(int i=0; i<cycles; i++){
char* cycle="--NEW CYCLE "+(char)i+"---\r\n";
writeText(cycle);
}
However, I get error C2110: '+' : cannot add two pointers at the line where I declare cycle. How can I declare the the variable so that I can include the cycle number, i and also add a string to both sides?
Thanks for the help!
That’s not how you concatenate strings in C++.
You can either use
sprintforstd::string.The latter is more C++-ish::
Note that you must also change the function signature to:
since
string::c_str()returns aconst char*.Or you could use a
stringstream, but that’s a bit of overkill in this case. Worth looking into it though.The reason you’re getting an error is that
"--NEW CYCLE "is aconst char*in C++, not astd::string.You could edit your line to work:
but I wouldn’t do that. It looks ugly.