sprintf_s correct usage

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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.

How am I using it incorrectly?
Last edited on
sprintf_s is a C function. It doesn't know what a C++ string is, so don't feed it C++ strings.

1
2
	sprintf_s(buffer, sizeof(buffer), "%s->%i->%s->%s()", sFile.c_str(),
		      nLine, sFunc.c_str(), sCurrFunc.c_str());


Better yet, don't use sprintf_s at all:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <string>

std::string genThread(std::string fileName, int line, const std::string& funcName)
{
    const char* arrow = "->";

    auto index = fileName.find_last_of('\\');

    if (index != std::string::npos)
        fileName.erase(0, index + 1);

    return fileName + arrow + std::to_string(line) + arrow + 
           funcName + arrow + "success";
}
Last edited on
Thanks for your attention and suggestions.

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.

Eagerly awaiting your response.
1) auto deduces the type of the variable from the type of the initializer. C++11.
http://en.cppreference.com/w/cpp/language/auto

2) npos indicates the find was not successful
http://www.cplusplus.com/reference/string/string/npos/

3) to_string is an overloaded function that returns a string representation of the value passed to it. C++11.
http://www.cplusplus.com/reference/string/to_string/

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.
Last edited on
I did, thanks.
Topic archived. No new replies allowed.