I have written program to read from usb port (which is not connected to anything) .From java I am passing int filedescriptor and byte array and in jni I am converting byte array to char* and using read().
jbyte *bufferPtr2 = (*env)->GetByteArrayElements(env, buf, NULL);
unsigned char* d_data2 = (unsigned char*)bufferPtr2;
n = read(fd, d_data2, lengthOfArray);
After executing , n=1 but d_data2 is empty. Why is this? Does read() read null character as data?
Per 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
.
The actual value of the bytes that are read does not matter.
So yes, read()
will read NUL
bytes.