c - 如何从 char 数组中检索 n 个字符

标签 c arrays char

我在 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/

相关文章:

c++ - 在声明时将静态二维数组初始化为零

c# - 如何检查阿拉伯/波斯字符是否为小写?

java - Java中使用递归查找数组中最大的元素

jquery - 使用 JSON 在完整日历上呈现事件

C编程警告: array subscript has type 'char' [-Wchar-subscripts]

mysql - MySQL中如何对字符串进行整数运算?

c - 如何使用没有按钮的 gtk 消息对话框执行操作

c++ - 跨平台,具有异步能力的 C/C++ HTTP 库

c - 错误 C2099 : initializer is not a constant & warning C4013: 'gamma' undefined; assuming extern returning int

iphone - 对 NSMutableArray 进行排序,然后对另一个 NSMutableArray 进行排序