copying a CString to array

Hi,

I have a code as given below. I've declared 2 CString with names m_movex and m_movey . Also as shown in the code, I have 2 arrays with names locationX[10] and locationY[10] . Now I need to copy the m_movex to locationX[10] and m_movey to locationY[10] . I tried different possibilities but it gives a error.

Can you please highlight?

CODE:

if(M==0)
{
char locationX[10];
char locationY[10];


CSerialPort movem1;
movem1.OpenPort("com1");
movem1.ConfigurePort(CBR_9600, 7, true, EVENPARITY , ONESTOPBIT);
movem1.SetCommunicationTimeouts(0,0,0,50,50);

char b[] = "MP 2";
for( int i=0; i< 30 ;i++)
{
char a = b[i];
movem1.WriteByte(a);
if ( a == '\0')
break;
}
-------------------------------------------------

COPY SHOULD BE DONE HERE

-------------------------------------------------


char c[] = "\r";
for( int i=0; i< 30 ;i++)
{
char a = c[i];
movem1.WriteByte(a);
if ( a == '\0')
break;
}
movem1.ClosePort();
}
The function you need is called strcpy. Or strncpy (strlcpy) for safer use.

 
char * strncpy ( char * destination, const char * source, size_t num );


using:

1
2
strncpy(locationX, m_movex, 9);
locationX[9] = '\0';


or: (if you have the possiblity)

1
2
// not official standard, but equivalent to the upper version
strlcpy(locationX, m_movex, 10);


Maikel
Last edited on
Topic archived. No new replies allowed.