File Reading Problem

I was trying to copy the text file data from sd card to an array. While trying in platformIO getting error for myFile.read() line. Below is the code snippet

FIle MyFile;
char buf[50];

myFile = SD.open("test.txt");

if (myFile) {

    int l = myFile.size();

    Serial.println(l);

    while (myFile.available()) {

      myFile.read(buf,50);

    }

    // close the file:

    myFile.close();

  } else {

    // If the file didn't open, print an error:

    Serial.println("error opening test.txt");

  }

it was showing error for the line myFile.read(buf,50);

And if I try to print myFile it works properly. Below is the code snippet for that

File myFile;

myFile = SD.open("test.txt");

  if (myFile) {

    int l = myFile.size();

    Serial.println(l);

    Serial.println("test.txt:");

    // read from the file until there's nothing else in it:

    while (myFile.available()) {

      Serial.write(myFile.read());

    }

    // close the file:

    myFile.close();

  } else {

    // If the file didn't open, print an error:

    Serial.println("error opening test.txt");

  }

Please provide any way to resolve the issue

…And what error is that exactly? With which platformio.ini? Not enough info.

What error are you seeing? Is it a compilation error? Or an error while the code is running. I’m asking because I don’t see any error trapping around your myFile.read(buf, 50) call. I suspect a compilation error.

Have you added any SD type libraries to your lib_deps entry in the platformio.ini file? It could be that you are using a library which is incompatible with the standard Arduino SD library and doesn’t have the function read which takes 2 parameters, a buffer and a count.

I’m afraid there’s not much more to go on until I know more about the errors and what you are set up to do. Thanks.

Cheers,
Norm.

Ok, here’s what I did. I took the Arduino SD example sketch, “DumpFile”, and imported it into PlatformIO using VSCode. I didn’t check the option to “use installed Arduino libraries” as I wanted to add whatever was required to make it work. I’m compiling for an Uno by the way.

The first compilation failed as I had no file SD.h. I added the folloiwng to the platformio.ini file:

lib_deps=
    SD

On recompiling, I got the option of around 5 different SD libraries compatible with my Uno:

Library Manager: Installing SD
Library Manager: Warning! More than one package has been found by SD requirements:
 - adafruit/SD @ 0.0.0-alpha+sha.041f788250
 - arduino-libraries/SD @ 1.2.4
 - rei-vilo/SD @ 0.0.0-alpha+sha.a8a1454af9
 - mbed-jackson-lv/SD @ 0.0.0+sha.405b46e831df
 - mbed-1529561000/SD @ 0.0.0+sha.db4599aff06a

Looking at the list, I saw that one of them was provided by “arduino-libraries” - so I chose that one. I updated platformio.ini as follows:

lib_deps=
    arduino-libraries/SD

This time, it compiled.

Looking at the code, I saw this bit where the data file is read and dumped out to the Serail Monitor:

  // if the file is available, write to it:
  if (dataFile) {
    while (dataFile.available()) {
      Serial.write(dataFile.read());
    }
    dataFile.close();
  }

As this is different to your problem, I changed it to this:

  // if the file is available, write to it:
  if (dataFile) {
    char buf[50];
    while (dataFile.available()) {
      dataFile.read(buf, 50);
      Serial.write(buf);
    }
    dataFile.close();
  }

This compiled happily. As did a complete clean up and:

  • The Adafruit version of SD compiled ok;
  • The rei-vilo/SD libraray did not compile as SD.h was not found.

The other two are MBED by the look of things, and they didn’t compile either.

So, which SD library are you using which doesn’t let you read a buffer full of data at a time I wonder?

Cheers,
Norm.