gpt4 book ai didi

c - 需要像 read() 这样的函数将整数数据读入缓冲区并获得与 read() 相同的缓冲区值

转载 作者:太空宇宙 更新时间:2023-11-04 09:57:49 28 4
gpt4 key购买 nike

当我输入相同的整数时,如何将整数转换为与使用 read(0,buff,nbytes) 获得的缓冲区相同的值/编码字符?我正在尝试编写类似 read() 的东西,但用整数数据代替读取到缓冲区的文件描述符参数。

like_read(int data,void *buff,size_t nbytes);

它应该类似于 read(),因为它应该读入缓冲区的值与 read(0,buff,nbytes) 从标准输入读入其缓冲区的值相同。例如,当我不首先使用 read(0,buff,nbytes) 直接提供整数地址作为缓冲区时

int Integer=25
int nbytes=2;
int rlen,wlen,wlen1;
int fd = open("ttyusb.txt", O_RDWR | O_CREAT,0777);
wlen = write(fd, &Integer, nbytes);
wlen1 = write(1, &Integer, nbytes);//for stdout
close(fd);

预期输出

25

实际输出/文件内容是一些编码字符

,它没有给我想要的结果,因为首先使用 read() 从 stdin 将整数读入缓冲区,然后使用 write() 将该缓冲区写入文件,例如:

int Integer;
int nbytes=2;
int rlen,wlen;
int fd = open("ttyusb.txt", O_RDWR | O_CREAT,0777);
rlen = read(0, &Integer, nbytes);
wlen = write(fd, &Integer, nbytes);
wlen1 = write(1, &Integer, nbytes);//for stdout
close(fd);

标准输入

25

预期输出

25

实际输出/文件内容

25

当我在 read(0, buffer, nbytes) 后打印缓冲区值时,它给出了一些编码值:

int Integer, nbytes=2;
int fd = open("ttyusb.txt", O_RDWR | O_CREAT,0777);
rlen = read(0, &Integer, nbytes);
wlen = write(fd, &Integer, nbytes);
wlen1 = write(1, &Integer, nbytes);
printf("\nInteger buffer value %d\n",Integer);
close(fd);

stdin 0 prints "Integer buffer value 2608", stdin 1 prints "Integer buffer value 2609", stdin 2 prints "Integer buffer value 2610",.....stdin 9 prints "Integer buffer value 2617"...

read() 使用什么编码来转换整数值,如果没有 read() 我该如何进行转换?

最佳答案

我不太确定您想要实现什么,但我的解释是您正在尝试将 int 的字节复制到缓冲区中。可以这样实现:

#include <string.h>

void like_read(int data, void *buff, size_t nbytes)
{
size_t n;

/* Limit number of bytes to copy from data variable. */
n = sizeof(data);
if (n < nbytes)
n = nbytes;
/* Copy bytes from data variable to the buffer. */
memcpy(buff, &data, n);
/* If destination buffer is larger than data variable, ... */
if (n < nbytes) {
/* ... set the remaining bytes in the destination to all zero. */
memset((char *)buff + n, 0, nbytes - n);
}
}

what encoding is read() using to convert integer values and how can I do that conversion without read()?

read() 只是从文件描述符中读取原始数据字节,不进行任何转换,至少在 POSIX 类型系统(例如 Unix 和 Linux)上是这样。 Microsoft Windows 等其他一些操作系统允许使用特殊标志打开文件,这些标志对原始数据字节进行一些转换,主要用于处理具有不同编码的文本文件。

like_read 函数中的 memcpy() 调用将原始数据字节从一个内存位置复制到另一个内存位置,而不进行任何转换。


编辑

我仍然不确定您 (OP) 想要实现什么,但根据您在下面的评论,也许您正在寻找一个函数,该函数将整数值作为十进制 ASCII 字符“打印”到带有换行符的缓冲区,但是截断为指定的字节数。这几乎可以通过 snprintf() 来完成,但是该函数总是将一个终止空字节写入缓冲区(如果缓冲区大小至少为 1),所以这不是我认为您想要的。因此,snprintf() 可用于将整数值打印到足够大小的中间缓冲区,然后将结果memcpy 打印到指定的缓冲区。这是一个实现:

#include <string.h>
#include <stdio.h>

void like_read(int data, void *buff, size_t nbytes)
{
char tempbuf[24]; /* ought to be large enough... */
size_t n;

/* Print data value to temporary buffer. */
n = snprintf(tempbuf, sizeof(tempbuf), "%d\n", data);
/* n is number of bytes that would be printed to tempbuf if room. */
/* Limit number of bytes to copy to size of temporary buffer. */
if (n > sizeof(tempbuf))
n = sizeof(tempbuf);
/* Limit number of bytes to copy to nbytes. */
if (n > nbytes)
n = nbytes;
/* Copy bytes from tempbuf to the buffer. */
memcpy(buff, tempbuf, n);
/* If destination buffer is larger than n, ... */
if (n < nbytes) {
/* ... set the remaining bytes in the destination to all zero. */
memset((char *)buff + n, 0, nbytes - n);
}
}

关于c - 需要像 read() 这样的函数将整数数据读入缓冲区并获得与 read() 相同的缓冲区值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58732404/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com