我在 C 应用程序中有一个 char 数组,我必须将它分成 250 个部分,以便我可以将它发送到另一个不能一次接受更多的应用程序。
我该怎么做?平台:win32。
最佳答案
来自 MSDN 文档:
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.
请注意,strncpy
不会检查有效的目标空间;留给程序员。原型(prototype):
字符 *strncpy(
字符 *strDest,
常量字符 *strSource,
size_t 计数
);
扩展示例:
void send250(char *inMsg, int msgLen)
{
char block[250];
while (msgLen > 0)
{
int len = (msgLen>250) ? 250 : msgLen;
strncpy(block, inMsg, 250);
// send block to other entity
msgLen -= len;
inMsg += len;
}
}
关于c - 如何从 char 数组中检索 n 个字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/162804/