Go Down

Topic: How to read array of bytes (Read 794 times) previous topic - next topic

MaikB85

Hello

I want to read and write an array of bytes to a file on sd card and I have some questions about it. The SD library supports the function write with parameters buf and len possible. That seems to be good for my plan.
But how does reading works? I don`t understand it exactly.

Is this the right way?:
Code: [Select]
CFile = SD.open("led01.txt");
  if (CFile)
  {
    for (int i=0; i<96; i++)
    {
      led01[i] = CFile.read();
    }
    CFile.close();
  } 


I want to fill the array led01 with the bytes of the file (96 bytes)

MaikB85

Today I get my Arduino MEGA and I have tested my code. It works great!

I think it works in this way: The first call of read() returns the first byte in the file. The next call of read() returns the next Byte and so on until there is nothing to read

greetings

CatweazleNZ

You can read SD card files line-by-line if this helps:

Code: [Select]
String l_line = "";
while (l_SDFile.available() != 0) {
    //A consistent line length will not lead to heap fragmentation
    l_line = l_SDFile.readStringUntil('\n');
    if (l_line == "") //no blank lines are anticipated
      break;
    //
 
   //Load into array here

}
   

Cheers

Catweazle NZ

liudr

Yes, read and write work the same way. The library keeps an internal pointer to where to read/write the next byte. All you need to do is repeatedly call read or write. Is your led01 array defined as a byte (or char) array?

Kostli


Today I get my Arduino MEGA and I have tested my code. It works great!

I think it works in this way: The first call of read() returns the first byte in the file. The next call of read() returns the next Byte and so on until there is nothing to read

greetings


Arduino mega? How did you conect the signals of the sd card to the pins of the arduino?

Go Up