C-从一定大小的缓冲区中读取字符串

发布于 2024-09-10 05:49:44 字数 191 浏览 5 评论 0原文

我有一个 char buf[x]int svoid* data

我想将一个大小为 s 的字符串从 buf 写入到 data 中。

我怎样才能实现它?

提前致谢。

I have a char buf[x], int s and void* data.

I want to write a string of size s into data from buf.

How can I accomplish it?

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

随遇而安 2024-09-17 05:49:44

假设

  • “字符串”指的是 C 语言中通常表示的以空字符结尾的字符串;
  • 您尚未在 data 中分配内存;
  • 您已经知道 s <= x

首先您需要在 data 中分配内存。不要忘记字符串末尾 0 字节的空间。

data = malloc(s+1);
if (data == NULL) {
    ... /*out-of-memory handler*/
}

假设 malloc 成功,您现在可以复制字节。

编辑

正如caf所指出的,这项工作的最佳功能是strncat(它是完全可移植的,是C89的一部分。)它附加到目标字符串,因此预先将目标安排为空字符串:

*(char*)data = 0;
strncat(data, buf, s);

其他较差的可能性,保留在这里作为相关函数的示例:

  • 如果您有 strlcpy (这是不是标准 C,但在现代 Unix 系统上很常见;有公共领域的实现):

    strlcpy(data, buf, s+1);
    
  • 如果你知道有源字符串中至少有 s 个字符,您可以使用 memcpy

    memcpy(data, buf, s);
    

    ((char*)data)[s+1] = 0;

  • 否则,您可以先计算源字符串的长度:

    size_t bytes_to_copy = strlen(buf);
    if (bytes_to_copy > s) bytes_to_copy = s;
    memcpy(数据,buf,bytes_to_copy);
    ((char*)数据)[s+1] = 0;
    
  • 或者您可以使用 strncpy,但如果源字符串的实际长度小得多,则效率较低比s

    strncpy(data, buf, s);
    ((char*)数据)[s+1] = 0;
    

Assuming that

  • by “string” you mean a null-terminated string as is normally meant in C;
  • you haven't yet allocated memory in data;
  • you already know that s <= x

First you need to allocate memory in data. Don't forget the room for the 0 byte at the end of the string.

data = malloc(s+1);
if (data == NULL) {
    ... /*out-of-memory handler*/
}

Assuming malloc succeeds, you can now copy the bytes.

EDIT:

The best function for the job, as pointed out by caf, is strncat. (It's fully portable, being part of C89.) It appends to the destination string, so arrange for the destination to be an empty string beforehand:

*(char*)data = 0;
strncat(data, buf, s);

Other inferior possibilities, kept here to serve as examples of related functions:

  • If you have strlcpy (which is not standard C but is common on modern Unix systems; there are public domain implementations floating around):

    strlcpy(data, buf, s+1);
    
  • If you know that there are at least s characters in the source string, you can use memcpy:

    memcpy(data, buf, s);
    

    ((char*)data)[s+1] = 0;

  • Otherwise you can compute the length of the source string first:

    size_t bytes_to_copy = strlen(buf);
    if (bytes_to_copy > s) bytes_to_copy = s;
    memcpy(data, buf, bytes_to_copy);
    ((char*)data)[s+1] = 0;
    
  • Or you can use strncpy, though it's inefficient if the actual length of the source string is much smaller than s:

    strncpy(data, buf, s);
    ((char*)data)[s+1] = 0;
    
耳钉梦 2024-09-17 05:49:44

如果没有分配data

char buf[] = "mybuffer";
void *data = malloc(strlen(buf)+1);
strcpy((char*)data,buf);

实际上如果确实要定义数据,你也可以这样做

char buf[] = "mybuffer";
void *data= (void*)strdup(buf);

If data is not allocated:

char buf[] = "mybuffer";
void *data = malloc(strlen(buf)+1);
strcpy((char*)data,buf);

Actually if data is really to be defined you can also do

char buf[] = "mybuffer";
void *data= (void*)strdup(buf);
绮筵 2024-09-17 05:49:44
memcpy(data, buf, s);

这假设您有足够的数据空间(和 buf)。

根据你正在做的事情(你没有说,但你确实说你正在复制字符串),如果你没有复制 buff 中已经存在的空值,你可能需要在新复制的字符串末尾添加一个空值,您将在需要字符串的函数中使用数据。

memcpy(data, buf, s);

This assumes that you have enough space in data (and in buf).

Depending on what you are doing (you don't say, but you do say that you are copying strings), you may want to add a null at the end of your newly copied string if you did not copy a null already in buff, and you are going to use data in a function that expects strings.

data = malloc(s);
strcpy((char*)data,buf);
free(data);
data = malloc(s);
strcpy((char*)data,buf);
free(data);
吃颗糖壮壮胆 2024-09-17 05:49:44
int n = MIN((x - 1), s);
char *bp = buf;
char *dp = (char *)data;
while (n--) {
  *bp++ = *dp++;
}
*dp = '\0';
int n = MIN((x - 1), s);
char *bp = buf;
char *dp = (char *)data;
while (n--) {
  *bp++ = *dp++;
}
*dp = '\0';
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文