I'm working with USB and I want to read the content of usb device descriptor in /dev/bus/usb/00x/00y - it is a character device.
I used fopen to open it as a binary file with "rb" parameter. But when I do the seek and tell to get file size, it returned 0 bytes in size. Is there a way to read it as a binary file?
void ReadUsbDeviceDescriptor( const char* path )
{
FILE* usb_fd = NULL;
size_t lSize = 0;
if ( path != NULL )
{
usb_fd = fopen ( path, "rb");
if ( usb_fd != NULL )
{
fseek( usb_fd , 0 , SEEK_END );
lSize = ftell (usb_fd);
rewind( usb_fd );
printf("File: %s - Size: %lu bytes\n", path, lSize);
fclose( usb_fd );
}
else
{
printf("Could not open file %s\n", path );
}
}
}
And here is the result:
File: /dev/bus/usb/001/001 - Size: 0 bytes
fseekis likely failing, if you check the resultcode ?fseekfailed. As @RalfFriedl said below, I can usefreadto read byte one by one to the end of the file, then get the length. Thank you for your help.