1
0
mirror of https://github.com/arduino/Arduino.git synced 2025-02-21 15:54:39 +01:00

Adding position() function to File class and replacing FILE_TRUNCATE and FILE_APPEND with FILE_WRITE (SD library). Updating examples and keywords.txt accordingly.

This commit is contained in:
David A. Mellis 2010-12-22 15:52:09 -06:00
parent 5057d5b109
commit 4742739d6e
7 changed files with 18 additions and 9 deletions

View File

@ -55,6 +55,10 @@ boolean File::seek(uint32_t pos) {
return SD.file.seekSet(pos); return SD.file.seekSet(pos);
} }
uint32_t File::position() {
return SD.file.curPosition();
}
uint32_t File::size() { uint32_t File::size() {
return SD.file.fileSize(); return SD.file.fileSize();
} }

View File

@ -297,6 +297,9 @@ boolean callback_openPath(SdFile& parentDir, char *filePathComponent,
if (isLastComponent) { if (isLastComponent) {
SDClass *p_SD = static_cast<SDClass*>(object); SDClass *p_SD = static_cast<SDClass*>(object);
p_SD->file.open(parentDir, filePathComponent, p_SD->fileOpenMode); p_SD->file.open(parentDir, filePathComponent, p_SD->fileOpenMode);
if (p_SD->fileOpenMode == FILE_WRITE) {
p_SD->file.seekSet(p_SD->file.fileSize());
}
p_SD->c = -1; p_SD->c = -1;
// TODO: Return file open result? // TODO: Return file open result?
return false; return false;

View File

@ -21,8 +21,7 @@
#include <utility/SdFatUtil.h> #include <utility/SdFatUtil.h>
#define FILE_READ O_READ #define FILE_READ O_READ
#define FILE_TRUNCATE (O_WRITE | O_CREAT | O_TRUNC) #define FILE_WRITE (O_READ | O_WRITE | O_CREAT | O_SYNC)
#define FILE_APPEND (O_WRITE | O_CREAT | O_APPEND)
class File : public Stream { class File : public Stream {
public: public:
@ -34,6 +33,7 @@ public:
virtual int available(); virtual int available();
virtual void flush(); virtual void flush();
boolean seek(uint32_t pos); boolean seek(uint32_t pos);
uint32_t position();
uint32_t size(); uint32_t size();
void close(); void close();
operator bool(); operator bool();

View File

@ -59,8 +59,9 @@ void loop()
} }
} }
// open the file: // open the file. note that only one file can be open at a time,
File dataFile = SD.open("datalog.txt", FILE_APPEND); // so you have to close this one before opening another.
File dataFile = SD.open("datalog.txt", FILE_WRITE);
// if the file is available, write to it: // if the file is available, write to it:
if (dataFile) { if (dataFile) {

View File

@ -46,7 +46,7 @@ void setup()
// open a new file and immediately close it: // open a new file and immediately close it:
Serial.println("Creating example.txt..."); Serial.println("Creating example.txt...");
myFile = SD.open("example.txt", FILE_TRUNCATE); myFile = SD.open("example.txt", FILE_WRITE);
myFile.close(); myFile.close();
// Check to see if the file exists: // Check to see if the file exists:

View File

@ -39,7 +39,7 @@ void setup()
Serial.println("initialization done."); Serial.println("initialization done.");
// open a file: // open a file:
myFile = SD.open("test.txt", FILE_TRUNCATE); myFile = SD.open("test.txt", FILE_WRITE);
// if the file opened okay, write to it: // if the file opened okay, write to it:
if (myFile) { if (myFile) {

View File

@ -19,11 +19,12 @@ remove KEYWORD2
rmdir KEYWORD2 rmdir KEYWORD2
open KEYWORD2 open KEYWORD2
close KEYWORD2 close KEYWORD2
seek KEYWORD2
position KEYWORD2
size KEYWORD2
####################################### #######################################
# Constants (LITERAL1) # Constants (LITERAL1)
####################################### #######################################
FILE_READ LITERAL1 FILE_READ LITERAL1
FILE_TRUNCATE LITERAL1 FILE_WRITE LITERAL1
FILE_APPEND LITERAL1