Hey,
I have two char variables, m_GPSOffset[13] and m_FileName[100]. When m_GPSOffset has a value assigned to it, say for instance +11:25:30. The first entry of the value, in this case +, is always stored in m_FileName. I am clueless on why this is occurring. Can anyone tell me why this is happening?
AFAIK member function GetLength returns the number of characters in a CString object does not counting the null-terminating character. So it can be that the returned buffer is not null-terminated.
Early you were using m_strGPSOffsetTm but in the last post you are using m_InitFileName.
So it looks like that you do not understand what you are doing.
Class CString has method operator const char *. So you can write simply
Simple CTest class with corresponding CResource class to attempt to demonstrate what you are attempting to do. But in the CResource class I have initialised the char arrays in the constructor and also check that we don't overrun the buffer when we set one of these arrays. Does that help?
CString s = _T( "Hello World" );
std::wcout << static_cast<constwchar_t *>( s ) << std::endl;
std::wcout << "Number of characters is " << s.GetLength() << std::endl;
wchar_t t[12];
wcscpy( t, s.GetBuffer( 2 ) );
if ( t[0] == L'H' && t[1] == L'e' ) wcout << "There is no the terminating zero" << std::endl;
As you see when the size of the buffer is equal to or less then the value returned by GetLength then the terminating zero is not present. So the function strcpy has undefined behaviour. I showed you two methods how you can copy a CString object or you can use strncpy function instead of strcpy and append the terminating zero yourself.
Yes, and strcpy_s will check that you don't exceed the buffer size, as I demonstrated above. If the string being copied is 1 char less than you have declared the array into which you are copying all will be fine, but if it is the same or larger an assertion will be thrown.
ajh32,
Thanks for replying. I am still having the same issue even after changing my code to exactly what you have. Did you have the same issue I had? Once again thanks!!
Thanks for replying. I am still having the same issue even after changing my code to exactly what you have. Did you have the same issue I had? Once again thanks!!
No. Did you initialise your char arrays first? Are you using a safe string copy?