Hey there,
I'm having a bit of trouble with sprintf_s(). I keep getting a currupted format into my buffer when I use it and an Access Violation. I assume I'm using it incorrectly.
I'm making a function that checks the values of variables throughout my code and then tells me if there is a bug and where it occured.
std::string genThread(std::string fileName, int line, std::string funcName)
{
// Working Variables
//---------------------------------------------------------
char buffer[256] = {'\0'};
std::string sFile(fileName);
int i(sFile.length());
int nLine(line);
std::string sFunc(funcName);
std::string sCurrFunc("success");
// Find last '\\'
while (sFile[i] != '\\')
{
--i;
}
// Remove up until file name
sFile.erase(0, i + 1);
// Format the caption
sprintf_s(buffer, sizeof(buffer), "%s->%i->%s->%s()", sFile,
nLine, sFunc, sCurrFunc);
// Return the caption
return std::string(buffer);
}
The above code is a function that generates the caption for my message box when it encounters a bug.
Within that function, sprinf_s is not working properly.
I'm interested in a few things you have done to modify my code.
1. What is an "auto"?
I've never used it myself.
2. I'm not sure what the condition is being tested within the if() statement.
What is "std::string::npos"?
3. What is a "std::to_string"?
One last thing...
4. I noticed you changed the function signature, so that the last
parameter is a "const std::string&" and have left the first parameter as
"std::string". Is there a specific reason you have done this?
Thank you again, for your time.
I'll do my own research through documentation on these subjects.
But I will still greatly value your explanations.
4) The first parameter is passed by value since the function modifies the passed copy of the variable (line 10). The last parameter is passed by const reference since it is not changed. It is more efficient to pass a string by reference than by value. The const ensures that the function does not change the value of the argument since it refers to the caller's variable.
Most of these questions, you could have found the answer right here on this site by using the site's search function.