Passing file to a function

Hello,
In this code I need to pass file to the function and then read it inside the function.
The problem I can not read anything. it print "Opening file to read failed"
I am using ESP32 CAM

#include "SD_MMC.h"

void printpassf(File &f){
  
  File file2 = SD_MMC.open("/f.txt", FILE_READ);
  if (!file2) {
    Serial.println("Opening file to read failed");
    return;
  }
  Serial.println("File Content:");
  while (file2.available()) {
    Serial.write(file2.read());
  }
}
 
void setup() {
  Serial.begin(115200);

  if (!SD_MMC.begin()) {
    Serial.println("Failed to mount card");
    return;
  }
 
  File file = SD_MMC.open("/test.txt", FILE_WRITE);
 
  if (!file) {
    Serial.println("Opening file to write failed");
    return;
  }
  if (file.print("Test file write")) {
    Serial.println("File write success");
  } else {
    Serial.println("File write failed");
  }
  printpassf(file);

  file.close(); 

} 
void loop() {}

Please read:

Which arduino are you using, and which link of this SD_MMC library?

Drop the / prefix on the file name for a start.

Now it print (??????) in loop.

Yes I did that.
the file should contain "Test file writ " but when I opened the file, the file is empty!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.