Skip to content

SD File Reuse Snippet #73

@mattgaidica

Description

@mattgaidica

@KravitzLab looking at the current function, it appears it could scan a file to infinity (as long as there are newlines)? Here's a suggestion to stop once the target number of lines are met, see while (file.available() && lineCount < 3):

void FED3::getFilename(char *filename) {
  DateTime now = rtc.now();
  filename[3] = FED / 100 + '0';
  filename[4] = FED / 10 + '0';
  filename[5] = FED % 10 + '0';
  filename[7] = now.month() / 10 + '0';
  filename[8] = now.month() % 10 + '0';
  filename[9] = now.day() / 10 + '0';
  filename[10] = now.day() % 10 + '0';
  filename[11] = (now.year() - 2000) / 10 + '0';
  filename[12] = (now.year() - 2000) % 10 + '0';
  filename[16] = '.';
  filename[17] = 'C';
  filename[18] = 'S';
  filename[19] = 'V';
  for (uint8_t i = 0; i < 100; i++) {
    filename[14] = '0' + i / 10;
    filename[15] = '0' + i % 10;
    if (SD.exists(filename)) {
      // Open the file to check its length
      File file = SD.open(filename, FILE_READ);
      if (file) {
        int lineCount = 0;
        while (file.available() && lineCount < 3) {
          if (file.read() == '\n') {
            lineCount++;
          }
        }
        file.close();
        // If the file has less than 3 lines, delete it
        if (lineCount < 3) {
          SD.remove(filename);
          break;
        }
      } else {
        Serial.println("Error opening file for reading.");
      }
    } else {
      // File doesn't exist, use this filename
      break;
    }
  }
  return;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions