需要像 read() 这样的函数将整数数据读入缓冲区并获得与 read() 相同的缓冲区值
Need a function like read() that reads integer data into buffer and obtains same buffer value as read()
当我输入相同的整数时,如何将整数转换为与使用 read(0,buff,nbytes) 获得的缓冲区相同的 value/encoded 字符?我正在尝试编写类似 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
实际output/file内容是一些编码字符
,它没有给我想要的结果,因为首先使用 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
实际output/file内容
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) 想要实现什么,但根据你在下面的评论,也许你正在寻找一个函数 "prints" 将整数值作为十进制 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);
}
}
当我输入相同的整数时,如何将整数转换为与使用 read(0,buff,nbytes) 获得的缓冲区相同的 value/encoded 字符?我正在尝试编写类似 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
实际output/file内容是一些编码字符
,它没有给我想要的结果,因为首先使用 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
实际output/file内容
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) 想要实现什么,但根据你在下面的评论,也许你正在寻找一个函数 "prints" 将整数值作为十进制 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);
}
}