AnsweredAssumed Answered

Problems when reading large files from USB stick.

Question asked by Manuel Malagon on Oct 21, 2019
Latest reply on Oct 23, 2019 by Daniel Chen

I'm facing problems when reading large files. I have some code that copies one file from a USB stick to an SD Card. But I'm facing issues when copying large files, larger than 10MB. Here is how I'm doing it:

 

  1. I open the file I want to copy in the "source" media as "r"
  2. I open a file on the "dest" device with attributes as "wb"
  3. I allocate an 8192 byte buffer that is the one I will use for reading from the source file and writing to the destination file.
  4. I read 8192 bytes from the source file and write them to the destination file. I do this as many times as bytes there are.

 

Everything goes well for the first couple of megs, but then randomly, the "read" function returns 0 and the source device stops responding, even when I close the file and try to open it again. I have done this copying successfully with files under 10 MB but it doesn't work with files larger than that. I'm reading from USB and immediately writing to the SD Card without interruption or delays, I don't know if I need to introduce a delay in between read and write or something.

 

What I'm trying to know if this is the correct way of doing it, just opening both files, read from one and write to the other. I mean, it works just fine for files up to 10MB so I'm not sure.

Outcomes