Arduino Forum upgrade scheduled for Monday, October 20th, 11am-4pm (CEST). Sorry for the inconvenience!
Pages: [1]   Go Down
Author Topic: SD Library: Only 31854 bytes available from a 160KB file  (Read 1288 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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?
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 213
Posts: 9060
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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"
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

0
Offline Offline
Edison Member
*
Karma: 67
Posts: 1703
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

0
Offline Offline
Edison Member
*
Karma: 67
Posts: 1703
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

0
Offline Offline
Full Member
***
Karma: 0
Posts: 239
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley
Logged

Pages: [1]   Go Up
Arduino Forum upgrade scheduled for Monday, October 20th, 11am-4pm (CEST). Sorry for the inconvenience!
Jump to: