Sample Header Ad - 728x90

How to open and read a character device as binary file?

1 vote
1 answer
1230 views
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
Asked by Manh Huynh (13 rep)
May 21, 2021, 10:06 AM
Last activity: May 21, 2021, 01:00 PM