logo elektroda
logo elektroda
X
logo elektroda

Arduino IDE ESP8266 SD.h - WRITE, READ, APPEND file opening modes - unclear operation

zbynio_k 501 10
ADVERTISEMENT
Treść została przetłumaczona polish » english Zobacz oryginalną wersję tematu
  • #1 21648743
    zbynio_k
    Level 10  
    Hello,
    patient - Arduino IDE, esp8266, SD card, SD.h library, operations on (text) file
    Can someone explain this behaviour of the library to me?
    1. opening a file in WRITE mode adds a line to the file, not WRITE
    2. same as when opening { FILE_WRITE | O_APPEND }
    3. after opening a WRITE file, performing the operation and closing the file - dataFile.close();
    attempt to reopen ends with an error
    4. after opening the WRITE file, both data can be written and read
    Syntax - File dataFile = SD.open(file1, FILE_WRITE);

    every "normal" library (e.g. in Delphi)
    distinguishes between WRITE, READ and APPEND file opening modes

    Am I making a mental error, or does this library just work like that?
  • ADVERTISEMENT
  • #3 21648778
    zbynio_k
    Level 10  
    Thank you,
    after a cursory review
    I am not using SD.h, but some Fat or PetiteFat from the FS.h library
  • ADVERTISEMENT
  • ADVERTISEMENT
  • #5 21649080
    zbynio_k
    Level 10  
    Well, unfortunately it doesn't work like that,
    it does not compile, even though there should be downward compatibility
    I installed ESP32 board > it doesn't compile even when ESP32 board is selected
    ESP Espressif does not want to install
    thank you for your interest
  • ADVERTISEMENT
  • #6 21649115
    inot
    Level 38  
    I can see on my old computer how I used to solve this. If I recall correctly, the problems were with different versions of Arduino Core.

    I checked the working program.
    Actually the APPEND mode was not working, so I used the code below:
    Code: Arduino
    Log in, to see the code
    .
  • #7 21649519
    JacekCz
    Level 42  
    zbynio_k wrote:
    No, unfortunately that's not how it works,
    it doesn't compile, even though there should be downward compatibility


    Congrats on the unambiguous posting of the code and the compiler message.
  • #8 21650774
    zbynio_k
    Level 10  
    inot wrote:
    I can see on my old computer how I used to solve this. If I recall correctly, the problems were with different versions of Arduino Core.

    I checked the working program.
    Actually the APPEND mode was not working, so I used the code below:
    Code: Arduino
    Log in, to see the code
    .

    yes, seek(EOF) works instead of APPEND
    but SD.open(name, FILE_WRITE) also works like APPEND
    unfortunately in the SD.h library you have to delete the file and create it again

    thank you for your interest
  • #9 21650834
    inot
    Level 38  
    zbynio_k wrote:
    in the SD.h library you need to delete the file and create it anew
    .
    Why?
    With the code given above, you don't have to wonder if a new file or an existing file is open. You always jump to the end of the file (in the case of a new file, it's the beginning) and can continue saving. It works 100% of the time.
  • #10 21651400
    zbynio_k
    Level 10  
    unfortunately NO
    after the seek(0) is executed, the "cursor" does indeed move to the beginning of the file, where it saves the data, but those that were
    checked a couple of times
  • #11 21651414
    JacekCz
    Level 42  
    @zbynio_k you don't understand what kol @inot is talking about
    You are stubbornly talking about YOUR vision with seek(), and that is the correct operation of seek(), there is no silent "trunc"

Topic summary

The discussion addresses the behavior of the Arduino IDE SD.h library when used with ESP8266 for file operations on an SD card. The main issue is the unexpected behavior of the FILE_WRITE mode, which appends data to the file rather than overwriting it, similar to the combined FILE_WRITE | O_APPEND mode. Additionally, reopening a file after closing it in WRITE mode results in an error, and files opened in WRITE mode allow both reading and writing, which differs from typical file mode distinctions in other environments like Delphi. A follow-up clarifies that the user is not using the SD.h library but rather a Fat or PetiteFat implementation from the FS.h library, which may explain the observed behavior. The discussion highlights differences in file mode semantics between Arduino SD libraries and conventional file handling libraries.
Summary generated by the language model.
ADVERTISEMENT