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 !
- #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:
- }