Archived

This topic is now archived and is closed to further replies.

zackriggle

Using strncpy()

Recommended Posts

Alright, I just want to know if strncpy() stops if it finds a null-terminator in the source string. My reason for this is that I may need a BYTE (u_char) pointer to store binary data at some time (actually, I know I will need it) and want to be able to copy that data to another BYTE buffer. Should I just make my own function that uses a for() loop and copies all of the data (or as much data as specified by a length parameter)? Or, will strncpy() work?

Share this post


Link to post
Share on other sites
quote:

The strncpy function copies the initial count characters of strSource to strDest and returns strDest. If count is less than or equal to the length of strSource, a null character is not appended automatically to the copied string. If count is greater than the length of strSource, the destination string is padded with null characters up to length count. The behavior of strncpy is undefined if the source and destination strings overlap.

Share this post


Link to post
Share on other sites
Okay then, my own function it is. Someone please just glance at this and tell me if it will work:

typedef unsigned char BYTE
typedef unsigned long DWORD
void bincpy(BYTE* dest, const BYTE* source, DWORD length)
{
for(DWORD x = 0; x != length; x++)
dest[x] = source[x];
};


This is supposing that dest and source are the exact same length

Share this post


Link to post
Share on other sites
Why don''t you try memcpy. It copies memory(or rather data) from one place to another.

void* memcpy(void* dest, const void* src, size_t num);

Share this post


Link to post
Share on other sites