Go Down

Topic: SD Library: Only 31854 bytes available from a 160KB file (Read 1 time) previous topic - next topic

pjgat09

I am trying to use the SD library to read from a file on my card and use the contents. I copied a 160KB file to the card and opened it with the SD library, but the available() function says there are only 31854 available bytes.

Here is the code that tells me that:
Code: [Select]
#include <SD.h>

File data;

void setup()
{
    SD.begin(10);

    Serial.begin(9600);
   
    data = SD.open("file.txt");
   
    if (data)
    {
      Serial.println(data.available());
    }
}

void loop()
{     
   
}


If I use the read() function in a loop until there are no more bytes available, the program does only read 31854 bytes. How do I access all the data in my file?

johnwasser

Is it possible that the library is using an unsigned int where it should be using an unsigned long?

31,854 = 0x7C6E

162,926 = 0x00027C6E = 159.1 * 1024 = "160k"
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

fat16lib

It's a bug in SD.h, the wrapper for SdFat.  available() does the 32-bit difference of filesize - current_position and incorrectly returns a 16-bit result.

pjgat09

Thanks for the replies. I see the bug in the source for the library now. It seems I can avoid it by doing the subtraction myself.

Should I report this bug so it can be fixed in the next revision, and where would be best to do that?

fat16lib

I sent an email to the person who maintains the SD library wrapper.

ladyada

Hi this is in fact an issue, unfortunately we cannot fix it while the virtual Stream object requires returning only a 16 bit int

until then, please use "size() - position()" which will give you the remaining bytes :)

Go Up