c - c中的read()是否读取空字符

标签 c serial-port java-native-interface

我已经编写了从 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 read nbyte bytes from the file associated with the open file descriptor, fildes, into the buffer pointed to by buf.

读取的字节的实际值无关紧要。

是的,read() 将读取 NUL 个字节。

关于c - c中的read()是否读取空字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50625105/

相关文章:

c - 将类型定义的二维结构数组作为函数参数传递

c - for 循环在 C 中不能正确递增

ruby serialport gem,谁负责检查奇偶校验错误?

serial-port - PuTTY 串行连接重新连接

android - Android Studio OpenCV 3.2,无法使用 native OpenCV 打开视频文件或安卓相机

c - 为 Ceasar's Code 在 C 中环绕

c - vscode调试后如何自动关闭任务终端

python - 从 Arduino 到 Python 的连续数据流(读取线失败)

Android 如何读取 JNI 核心转储文件

Android JNI,如何使用soname libxx.so.1.2.3加载库