WCHAR copy one char at a time
-
Windows 7, Visual Studio, C++, MFC and non console type applications that have no windows Given: WCHAR destination[ 60 ] WCHAR source[ 60 ] Presume: destination has 20 characters to be retained. What is the best method of copying all the characters that will fit from source into destination? Easy enough for ASCII, but difficult for this WCHAR novice.
Thanks for your time If you work with telemetry, please check this bulletin board: http://www.bkelly.ws/irig\_106/
-
Windows 7, Visual Studio, C++, MFC and non console type applications that have no windows Given: WCHAR destination[ 60 ] WCHAR source[ 60 ] Presume: destination has 20 characters to be retained. What is the best method of copying all the characters that will fit from source into destination? Easy enough for ASCII, but difficult for this WCHAR novice.
Thanks for your time If you work with telemetry, please check this bulletin board: http://www.bkelly.ws/irig\_106/
-
Refer back to the OP. The strings have the same maximum length. String destination already has 20 characters so 60 more will not fit. Function wcscpy() has only two arguments, destination and source. We can write the destination in the format: destination[20] to start there, but there is no way to specify that only 39 of the source characters are to be copied. However, having seen that, I then found wcscpy_s() with three arguments. We can specify destination[ 20 ], then specify the second argument would be 40. We can generalize this with a calculated length. But I am unsure about the right functions to use with WCHAR to get the max length and to get the current length. I will try to find that and post again.
Thanks for your time If you work with telemetry, please check this bulletin board: http://www.bkelly.ws/irig\_106/
-
Refer back to the OP. The strings have the same maximum length. String destination already has 20 characters so 60 more will not fit. Function wcscpy() has only two arguments, destination and source. We can write the destination in the format: destination[20] to start there, but there is no way to specify that only 39 of the source characters are to be copied. However, having seen that, I then found wcscpy_s() with three arguments. We can specify destination[ 20 ], then specify the second argument would be 40. We can generalize this with a calculated length. But I am unsure about the right functions to use with WCHAR to get the max length and to get the current length. I will try to find that and post again.
Thanks for your time If you work with telemetry, please check this bulletin board: http://www.bkelly.ws/irig\_106/
Take a look at http://msdn.microsoft.com/en-us/library/xdsywd25.aspx[^], which allows you to specify the maximum number of characters. As a developer it is quite important to get to know all the CRT functions, and STL classes.
Veni, vidi, abiit domum
-
Take a look at http://msdn.microsoft.com/en-us/library/xdsywd25.aspx[^], which allows you to specify the maximum number of characters. As a developer it is quite important to get to know all the CRT functions, and STL classes.
Veni, vidi, abiit domum
Yes, you are so right. Found the page, got the function, using it. Thank you for taking the time to reply.
Thanks for your time If you work with telemetry, please check this bulletin board: http://www.bkelly.ws/irig\_106/
-
Windows 7, Visual Studio, C++, MFC and non console type applications that have no windows Given: WCHAR destination[ 60 ] WCHAR source[ 60 ] Presume: destination has 20 characters to be retained. What is the best method of copying all the characters that will fit from source into destination? Easy enough for ASCII, but difficult for this WCHAR novice.
Thanks for your time If you work with telemetry, please check this bulletin board: http://www.bkelly.ws/irig\_106/
Not sure what you mean by 20 characters needing to be retained. No need to walk the string one-by-one...
wcsncpy_s(destination, 60, source, 20);
Will take the first 20 characters of source and copy them to destination. But if you must walk it one-by-one are several ways...
for(i=0; i < 60; i++)
{
WCHAR *current = &source + (sizeof(WCHAR) * i); // one way
destination[i] = *current;WCHAR current = source\[i\]; // another way destination\[i\] = current;
}
I'd never declare a variable in a loop like that, so it's just for illustration. Anyway, you can do whatever logic you want to skip the first 20 chars in destination at that point.
Jeremy Falcon