|
-
- #include <SPI.h>
- #include "SdFat.h"
- #include "sdios.h"
-
-
- const uint8_t chipSelect = SS;
-
- SdFat sd;
-
- ArduinoOutStream cout(Serial);
-
-
- #define error(s) sd.errorHalt(F(s))
-
- void demoFgets() {
- char line[25];
- int n;
-
- SdFile rdfile("fgets.txt", O_RDONLY);
-
-
- if (!rdfile.isOpen()) {
- error("demoFgets");
- }
-
- cout << endl << F(
- "Lines with '>' end with a '\\n' character\n"
- "Lines with '#' do not end with a '\\n' character\n"
- "\n");
-
-
- while ((n = rdfile.fgets(line, sizeof(line))) > 0) {
- if (line[n - 1] == '\n') {
- cout << '>' << line;
- } else {
- cout << '#' << line << endl;
- }
- }
- }
-
- void makeTestFile() {
-
- SdFile wrfile("fgets.txt", O_WRONLY | O_CREAT | O_TRUNC);
-
-
- if (!wrfile.isOpen()) {
- error("MakeTestFile");
- }
-
-
- wrfile.print(F(
- "Line with CRLF\r\n"
- "Line with only LF\n"
- "Long line that will require an extra read\n"
- "\n"
- "Line at EOF without NL"
- ));
- wrfile.close();
- }
-
- void setup(void) {
- Serial.begin(9600);
-
-
- while (!Serial) {
- SysCall::yield();
- }
-
- cout << F("Type any character to start\n");
- while (!Serial.available()) {
- SysCall::yield();
- }
- delay(400);
-
-
-
- if (!sd.begin(chipSelect, SD_SCK_MHZ(50))) {
- sd.initErrorHalt();
- }
-
- makeTestFile();
-
- demoFgets();
-
- cout << F("\nDone\n");
- }
- void loop(void) {}
|