Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

File.seek() in SD library--version 1.2.2 didn't work on M0 Pro using the IDE on macOS #50

Open
egienvalue opened this issue Mar 2, 2018 · 4 comments
Labels
topic: code Related to content of the project itself type: imperfection Perceived defect in any part of project

Comments

@egienvalue
Copy link

Test platform: Arduino M0 Pro

I try to use the seek() function to move the write pointer to the head of a file, it didn't work. Then I change the version to 1.2.0, and it works with the same code.

@cmaglie
Copy link
Contributor

cmaglie commented Mar 5, 2018

Please post a test sketch to show the problem.

@jmpmscorp
Copy link

I'm at the same the point. I test it with 1.2.1 and same behaviour. However, with 1.2.0 it works. I'm not testing with M0 but with a SAMD21 compatible board.

#include "SD.h"
#define SD_CS_PIN 51
File file;

void setup() {
   pinMode(SD_CS_PIN, OUTPUT);
   SD.begin();
}

void loop() {
   file = SD.open("test.txt", FILE_WRITE);
   unsigned long filesize = file.size();
   
  if(filesize == 0){
    file.print('[');
  }
  else{
    file.seek(filesize - 1);
    file.write(',');
  }
  file.print("{\"test\": true}");

  file.write(']');

  file.close();
}

My code is too much large to post here. I think this part of code summarize the problem. If you need something more, tell me.

Thank you, best regards.

@agrunte
Copy link

agrunte commented Mar 21, 2018

seek() does not work indeed, if the file is opened with FILE_WRITE. But it works if the file is opened with O_WRITE instead: file = SD.open("test.txt", O_WRITE);

@jmpmscorp
Copy link

Using version 1.2.0 while my requisites were solved, now, I have had to upgrade to 1.2.2 because it was solved any other problem I had with 1.2.0.

Following @agrunte post and another post I could find on google, I can work with seek() function since now, but I should advice something.

With this version, it seems that, when you open file with FILE_WRITE = (O_READ | O_WRITE | O_CREAT | O_APPEND), this last O_APPEND force file to write always at last position althougth you use seek() function. If you open file with only O_WRITE, if file doesn't exist previosly on SD, it will never be created and it doesn't be write. So, to work with seek() without problem, you could open file with every clause except O_APPEND.

file = SD.open("test.txt", O_READ | O_WRITE | O_CREAT);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: code Related to content of the project itself type: imperfection Perceived defect in any part of project
Projects
None yet
Development

No branches or pull requests

5 participants