SD card and file seek problem

ESPfri2
Posts: 9
Joined: Tue May 07, 2019 3:52 pm

SD card and file seek problem

Postby ESPfri2 » Tue May 07, 2019 3:59 pm

Hello
the following code end up with a read returning -1
I don't see what I am missing here ... :(
if you have ideas, thanks much !

Code: maib.cpp Select all


#include <Arduino.h>
#include <SPI.h>
#include <SD.h>
#include <errno.h>
#include <WiFi.h>
#include <time.h>
#include <sys/time.h>
#include <Wire.h>
#include <EEPROM.h>

struct Files_t {
const char *name;
File file;
};

struct Files_t f1 = { "/f1.txt"};
struct Files_t f2 = { "/f2.txt"};
struct Files_t f3 = { "/f3.txt"};

struct Files_t *Files[] = { &f1, &f2, &f3, NULL };

#define _BUFFERMAX 128
static char _Buffer[_BUFFERMAX]="";
char *LogPrintln ( const char * format, ... )
{
va_list args;
va_start (args, format);
vsnprintf (_Buffer, _BUFFERMAX, format, (va_list)args);
if (Serial)
Serial.println (_Buffer);
va_end (args);
return _Buffer;
}

void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);

delay(3000);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}

LogPrintln("Initializing SD card");

if (!SD.begin(5)) {
LogPrintln("... initialization failed");
while (1);
}
LogPrintln("initialization done");

LogPrintln("Openning file : %s", Files[0]->name);
Files[0]->file = SD.open(Files[0]->name, FILE_WRITE);

if (Files[0]->file) {
LogPrintln("Writing to file : %s", Files[0]->name);
Files[0]->file.println("line 1");
Files[0]->file.println("line 2");
Files[0]->file.println("line 3");
Files[0]->file.println("line 4");
} else
LogPrintln("error opening ");

LogPrintln("Current Position : %d", Files[0]->file.position());
Files[0]->file.flush();
Files[0]->file.seek(0, SeekSet);
LogPrintln("New Position : %d", Files[0]->file.position());
LogPrintln("Available data : %d", Files[0]->file.available());

LogPrintln("Reading file ...");

LogPrintln("%d", Files[0]->file.read());
LogPrintln("%d %s", errno, strerror(errno));
// while (Files[0]->file.available())
// Serial.write(Files[0]->file.read());

}

void loop() {
// put your main code here, to run repeatedly:
}

igrr
Espressif staff
Espressif staff
Posts: 2076
Joined: Tue Dec 01, 2015 8:37 am

Re: SD card and file seek problem

Postby igrr » Tue May 07, 2019 4:11 pm

Could the file mode be an issue?

SD.open(Files[0]->name, FILE_WRITE);

To read and write a file, r+ or w+ modes should be used.

ESPfri2
Posts: 9
Joined: Tue May 07, 2019 3:52 pm

Re: SD card and file seek problem

Postby ESPfri2 » Tue May 07, 2019 4:37 pm

in the file <FS.h> the only possible options for open are :

Code: Select all

namespace fs
{

#define FILE_READ       "r"
#define FILE_WRITE      "w"
#define FILE_APPEND     "a"

class File;
Here is the output of the above program :
initialization done
Openning file : /f1.txt
Writing to file : /f1.txt
Current Position : 32
New Position : 0
Available data : 32
Reading file
-1
9 Bad file number
everything looks fine except that read is not working as expected ...

ESPfri2
Posts: 9
Joined: Tue May 07, 2019 3:52 pm

Re: SD card and file seek problem

Postby ESPfri2 » Tue May 07, 2019 4:51 pm

thanks @ESp_iggr

I switched to from FILE_WRITE to "a+" , and no more error ...

Who is online

Users browsing this forum: PerplexityBot and 11 guests