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 !
  1. #include <Arduino.h>
  2. #include <SPI.h>
  3. #include <SD.h>
  4. #include <errno.h>
  5. #include <WiFi.h>
  6. #include <time.h>
  7. #include <sys/time.h>
  8. #include <Wire.h>
  9. #include <EEPROM.h>
  10.  
  11. struct Files_t {
  12.     const char  *name;
  13.     File    file;
  14. };
  15.  
  16. struct Files_t f1 = { "/f1.txt"};
  17. struct Files_t f2 = { "/f2.txt"};
  18. struct Files_t f3 = { "/f3.txt"};
  19.  
  20. struct Files_t *Files[] = { &f1, &f2, &f3, NULL };
  21.  
  22. #define _BUFFERMAX 128
  23. static char _Buffer[_BUFFERMAX]="";
  24. char *LogPrintln ( const char * format, ... )
  25. {
  26.     va_list args;
  27.     va_start (args, format);
  28.     vsnprintf (_Buffer, _BUFFERMAX, format, (va_list)args);
  29.     if (Serial)
  30.         Serial.println (_Buffer);
  31.     va_end (args);
  32.     return _Buffer;
  33. }
  34.  
  35. void setup() {
  36.   // Open serial communications and wait for port to open:
  37.   Serial.begin(9600);
  38.  
  39.   delay(3000);
  40.   while (!Serial) {
  41.     ; // wait for serial port to connect. Needed for native USB port only
  42.   }
  43.  
  44.   LogPrintln("Initializing SD card");
  45.  
  46.   if (!SD.begin(5)) {
  47.     LogPrintln("... initialization failed");
  48.     while (1);
  49.   }
  50.   LogPrintln("initialization done");
  51.  
  52.   LogPrintln("Openning file : %s", Files[0]->name);
  53.   Files[0]->file = SD.open(Files[0]->name, FILE_WRITE);
  54.  
  55.   if (Files[0]->file) {
  56.     LogPrintln("Writing to file : %s", Files[0]->name);
  57.     Files[0]->file.println("line 1");
  58.     Files[0]->file.println("line 2");
  59.     Files[0]->file.println("line 3");
  60.     Files[0]->file.println("line 4");
  61.   } else
  62.     LogPrintln("error opening ");
  63.  
  64.   LogPrintln("Current Position : %d", Files[0]->file.position());
  65.   Files[0]->file.flush();
  66.   Files[0]->file.seek(0, SeekSet);
  67.   LogPrintln("New Position   : %d", Files[0]->file.position());
  68.   LogPrintln("Available data : %d", Files[0]->file.available());
  69.  
  70.   LogPrintln("Reading file ...");
  71.  
  72.   LogPrintln("%d", Files[0]->file.read());
  73.   LogPrintln("%d %s", errno, strerror(errno));
  74. //  while (Files[0]->file.available())
  75. //    Serial.write(Files[0]->file.read());
  76.  
  77. }
  78.  
  79. void loop() {
  80.   // put your main code here, to run repeatedly:
  81. }

ESP_igrr
Posts: 1675
Joined: Tue Dec 01, 2015 8:37 am

Re: SD card and file seek problem

Postby ESP_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: No registered users and 22 guests