01-18-2006 10:31 PM
01-19-2006 04:49 AM
Although you have copied the 4 bytes you wanted, you did not terminate the string. So the printf() function will just continue reading from the string address you give it until it finds a 0 and then it will stop. An alternative function you might find useful is strncpy(), which will do a similar job to CopyBytes() but will automatically add the terminating 0 character after the copy operation.
JR
01-19-2006 09:42 AM
This function copies not more than a specified amount of characters (characters following an ASCII NUL byte are not copied) from a source string to a target string. If copying takes place between two objects that overlap, the behavior is undefined.Jacky has specified that 4 bytes should be copied from the source to the target. The terminating NULL was at the 5th byte. According to the help, strncpy will copy no more than 4 bytes to the target string.
If an ASCII NUL byte is found in the source buffer within the specified number of bytes, NUL bytes are written to the target until the specified number of characters have been filled in.
If no ASCII NUL byte is found within the specified number of bytes, the function returns after copying the specified number of bytes and does NOT append an ASCII NUL byte to the buffer.
If you want to guarantee that an ASCII NUL byte is at the end of the copied bytes, you can use the following:
strncpy (target, source, n);
target[n] = 0;
/*-------------------- Prototype ---------------------*/
char *strncpy (char Target_String[],
const char Source_String[], size_t Max_Chars);
01-19-2006 10:29 AM
I stand corrected! (Thought it would be a mistake to rely on my memory) ![]()
JR