while(dataFile.available()) not working?

I am trying to read from my sd card and the program doesn't run the code in this while, any alternatives also what does the .available() exactly do.
while(dataFile.available())
{

}

Thanks

campu0999:
I am trying to read from my sd card and the program doesn’t run the code in this while, any alternatives also what does the .available() exactly do.
while(dataFile.available())
{

}

Thanks

A little more code would be nice, so we could see what your ‘dataFile’ variable refers to. The available() function probably returns false in your situation, assuming that you have code inside your while loop, and not just an empty block, as in the code you posted above.

Please also use code tags, when posting code. Click the </> button to produce code tags, where you can paste your code into. The reason available() returns false() when it presumably should not, is impossible to find from your code example.

#include <SPI.h>
#include <SD.h>

  File dataFile;

  dataFile = SD.open("datalog.csv", FILE_WRITE);

if (dataFile)
   {
    String line = "";
    int readIndex = 0;
    while(dataFile.available())
    {
      line = dataFile.readStringUntil('\n');
      if(line == "")
      {
        Serial.println("break");
        break;
      }
      
      else
      {
        //parse here
        //removed for readability
        readIndex++;
      }     
    }
    dataFile.close();
  }

Sorry, hopefully this is enough. I tried to cut out the extra code. Thanks

When you open a file for writing, the file pointer is automatically positioned at the end of the file.
Try adding "dataFile.seek(0)" after you open the file.

I'm not guaranteeing that this is the problem, but I suspect that it might be so it's worth a try. :slight_smile:

Edit:

what does the .available() exactly do.

'available()' should return the number of bytes available. (The "Reference" is your friend.)

oldSteve thanks I have tried what you have said but now nothing gets printed to the serial?

campu0999:
… but now nothing gets printed to the serial?

Are you saying that before you did that you were getting “break” written to serial?
Or are you saying that nothing has changed?

Perhaps you should show all of your code, instead of just snippets.

void setup()  
{
    pinMode(9, INPUT);
    pinMode(10, OUTPUT);
  
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  Serial.print("Initializing SD card...");

  // see if the card is present and can be initialized:
//chipSelect is 53
  if (!SD.begin(chipSelect)) {
    Serial.println(F("Card failed, or not present"));
    // don't do anything more:
    return;
  }
  Serial.println(F("card initialized."));




   // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  dataFile = SD.open("datalog.csv", FILE_WRITE);
  //delay(1000);
  
  Serial.println("datafile opened");
  // if the file is available, read from it:
   if (dataFile)
   {
    //dataFile.seek(0);
    String line = "";
    int readIndex = 0;
    Serial.println(dataFile.available());
    while(dataFile.available() !=0)
    {
      Serial.println("datafile available");
      line = dataFile.readStringUntil('\n');
      if(line == "")
      {
        Serial.println("break");
        break;
      }
      //parse here
      else
      {
//eachLine is split into 2 strings
         Serial.println("sub string 1");
        str1[readIndex] = line.substring(0,9);            ////////////////////////////////////////////////////               
        Serial.println("sub string 2");
        str2[readIndex] = line.substring(11);                             
        Serial.println(str1[readIndex]);
        Serial.println(str2[readIndex]);
        readIndex++;
      }     
    }
    Serial.println(readIndex);
    dataFile.close();
  }

  // if the file isn't open, pop up an error:
  else {
    Serial.println("error opening datalog.csv");
  }
             

  
  delay(1000);
}

It seems like the file isnt being read at all as .available is always returning 0 even though the file has data.

campu0999:
It seems like the file isnt being read at all as .available is always returning 0 even though the file has data.

Even with the ‘seek(0)’ line uncommented?

If so, I can’t help further right now. I don’t have an SD card set up at the moment to replicate your problem, and don’t feel like doing it right now since it’s 9pm Sunday night. Maybe someone else can see what’s going wrong.

Just keep in mind that since the file was opened for writing, the file pointer is positioned at the end of the file, so it’s possible that ‘available()’ returns 0 because there is no more data in the file to be read. (Unless ‘available()’ ignores the position of the file pointer, but I doubt it.)

Good luck with it. (I’m going back to my Sunday night movie. :smiley: )

Even with the 'seek(0)' line uncommented?

The Serial monitor just freezes when it gets to that line.

campu0999:
The Serial monitor just freezes when it gets to that line.

That in itself is strange. Calling ‘seek()’ on an SD file shouldn’t affect serial comms or the serial monitor. It only positions the file pointer at the beginning of the SD card file. I’ve used it without problems in the past, followed by serial prints as shown here:-

        myFile.seek(0);                             // Move file pointer to beginning of file.
        if(myFile.find(searchString))               // Search for string.
        {
            Serial.print("\"");                     // String found - notify user.
            Serial.print(searchString);             //    "     "       "      "
            Serial.print("\"");                     //    "     "       "      "
            Serial.println(F(" was found"));        //    "     "       "      "
            // More code here

What SD card reader are you using? Have you tried opening the file for read only?