我已经编写了从 usb 端口(未连接到任何东西)读取的程序。我在 java 中传递 int filedescriptor 和字节数组,在 jni 中我将字节数组转换为 char* 并使用 read()。
jbyte *bufferPtr2 = (*env)->GetByteArrayElements(env, buf, NULL);
unsigned char* d_data2 = (unsigned char*)bufferPtr2;
n = read(fd, d_data2, lengthOfArray);
执行后,n=1 但d_data2 为空。为什么是这样? read() 是否读取空字符作为数据?
最佳答案
根据 the POSIX documentation for read()
:
The
read()
function shall attempt to readnbyte
bytes from the file associated with the open file descriptor,fildes
, into the buffer pointed to bybuf
.
读取的字节的实际值无关紧要。
是的,read()
将读取 NUL
个字节。
关于c - c中的read()是否读取空字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50625105/