|
-
- #include <SPI.h>
- #include "SdFat.h"
-
-
- const uint8_t chipSelect = SS;
-
-
- SdFat sd;
-
-
- ArduinoOutStream cout(Serial);
-
-
- #define error(s) sd.errorHalt(F(s))
-
- void demoFgets() {
- char line[25];
- int c;
- uint32_t pos;
-
-
- SdFile rdfile("fgets.txt", O_RDWR);
-
-
- if (!rdfile.isOpen()) {
- error("demoFgets");
- }
-
-
- cout << F("-----Before Rewrite\r\n");
- while ((c = rdfile.read()) >= 0) {
- Serial.write(c);
- }
-
- rdfile.rewind();
-
- while (1) {
- pos = rdfile.curPosition();
- if (rdfile.fgets(line, sizeof(line)) < 0) {
- error("Line not found");
- }
-
- if (strstr(line, "Line C")) {
- break;
- }
- }
-
-
- if (!rdfile.seekSet(pos)) {
- error("seekSet");
- }
- rdfile.println("Line R");
- rdfile.rewind();
-
-
- cout << F("\r\n-----After Rewrite\r\n");
- while ((c = rdfile.read()) >= 0) {
- Serial.write(c);
- }
-
-
- rdfile.close();
- }
-
- void makeTestFile() {
-
- SdFile wrfile("fgets.txt", O_WRITE | O_CREAT | O_TRUNC);
-
-
- if (!wrfile.isOpen()) {
- error("MakeTestFile");
- }
-
-
- wrfile.print(F(
- "Line A\r\n"
- "Line B\r\n"
- "Line C\r\n"
- "Line D\r\n"
- "Line E\r\n"
- ));
- wrfile.close();
- }
-
- void setup() {
- Serial.begin(9600);
-
-
- while (!Serial) {
- SysCall::yield();
- }
- cout << F("Type any character to start\n");
- while (Serial.read() <= 0) {
- SysCall::yield();
- }
-
-
-
- if (!sd.begin(chipSelect, SPI_HALF_SPEED)) {
- sd.initErrorHalt();
- }
-
- makeTestFile();
-
- demoFgets();
-
- cout << F("\nDone\n");
- }
- void loop() {}
|