Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In my vc++ code,

I want to change a filename xxx.000001.log to xxx.000002.log, xxx.000003.log.... using CString.

C++
struct idx_name{
    char type[4];
    char digit[6];
    char ext[4];
}

idx_name idx_logname;

	memset(&idx_logname, 0, sizeof(idx_logname));

        // get the char fname[100]; = "xxx.000001.log"
	memcpy(&idx_logname, fname, sizeof(idx_logname));

	CString s = CString(idx_logname.digit).GetBufferSetLength(6);
	int idx = _ttoi(s);
	idx++;

        ::ZeroMemory(&idx_logname.digit, sizeof(idx_logname.digit));
	sprintf_s(idx_logname.digit, sizeof(idx_logname.digit), "%06d", idx);
	::ZeroMemory(filename, sizeof(filename));
	memcpy(filename, &idx_logname, sizeof(idx_logname));

        // get Current_folder as current_directory
	CString folder = CString(Current_folder);
	s = folder + CString(filename);

This has no compile error.
But in the run time, process has running error at the line of "sprintf_s" like as

"Expression : ("Buffer too small",0);

As a novice of VC++, please let me know the error and who knows the simple ways to change filenames?

What I have tried:

One day wasted for this problem.
Posted
Updated 19-Jul-17 16:54pm
v2
Comments
Richard MacCutchan 20-Jul-17 4:28am    
One day wasted for this problem.
And you will continue to waste your time by guessing instead of spending some proper study time learning the language properly. Just about all the above code is wrong, and will lead to incorrect results at best, and application crashes at worst.

1 solution

Quote:
"Expression : ("Buffer too small",0);

You try to store a '/0' terminated string of size 6 in a buffer of size 6.
A '/0' terminated string of size 6 is 6+1, so it is 7.
Just for checking, try to use a larger buffer.
 
Share this answer
 
Comments
CPallini 20-Jul-17 2:17am    
5
Patrice T 20-Jul-17 3:12am    
Thank you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900