0

I am using libusb to interact with pen drive. We have to use the function int libusb_bulk_transfer(struct libusb_device_handle * dev_handle, unsigned char endpoint, unsigned char * data,int length,int * transferred,unsigned int timeout)
But here we specify only the end point So my question is that Is it actually possible to read write files(text or images) to pen drive. Or is it just for understanding? Please help! Code::

r = libusb_bulk_transfer(dev_handle, (2 | LIBUSB_ENDPOINT_OUT), data, 4, &actual, 0);
 //my device's out endpoint was 2, found with trial- the device had 2 endpoints: 2 and 129
    if(r == 0 && actual == 4) //we wrote the 4 bytes successfully
        cout<<"Writing Successful!"<<endl;
    else
        cout<<"Write Error"<<endl;
geocodezip
  • 158,664
  • 13
  • 220
  • 245
Vipul Goyal
  • 33
  • 2
  • 10

1 Answers1

3

Libusb works at a lower level than the filesystem. You're reading or writing raw blocks of data to/from the device, not dealing with things at the file level. If you formatted the device, saved a few files to it, then used your program to read lots of data off starting from near the beginning you'd likely eventually see the filenames, then data from the files, among a lot of other "gibberish" looking things.

If you want to read and write files this way, you're going to have to write code that can read that other data to figure out where on the device your files are, how to create new files, etc.

If you're just playing around, you can start at any arbitrary point on the flash drive, write a whole file to it, then read it back. But that'll only be understandable to your program, putting your flash drive in your desktop PC isn't going to know where to look to find it because you're missing the filesystem parts that tell it where your file is.

whamma
  • 8,088
  • 1
  • 14
  • 19
  • I appreciate your answer. But, you mentioned "starting from near the beginning " ,"start at any arbitrary point". How to do that? Because in the code , I have mentioned only the end point. I wrote some data to pen drive today. And when I tried to read it, it got stuck. I am doing this for a project, I need to show to my teacher where has the data gone? I have only started this, so I am sorry if what I am saying does not make any sense. – Vipul Goyal Nov 22 '12 at 13:29
  • 1
    You need to tell the device what you want to read/write before actually calling libusb_bulk_transfer(). You send a command first telling it that you're going to do a read or write, then wait for the reply. There are some examples that come with libusb that demonstrate this. You also might want to look at the USB mass storage specification for how to send/receive the commands at a low level: http://www.usb.org/developers/devclass_docs/usbmassbulk_10.pdf – whamma Nov 22 '12 at 15:55