You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

append.ino 1.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /*
  2. * Append Example
  3. *
  4. * This sketch shows how to use open for append.
  5. * The sketch will append 100 line each time it opens the file.
  6. * The sketch will open and close the file 100 times.
  7. */
  8. #include <SPI.h>
  9. #include <SdFat.h>
  10. // SD chip select pin
  11. const uint8_t chipSelect = SS;
  12. // file system object
  13. SdFat sd;
  14. // create Serial stream
  15. ArduinoOutStream cout(Serial);
  16. // store error strings in flash to save RAM
  17. #define error(s) sd.errorHalt(F(s))
  18. //------------------------------------------------------------------------------
  19. void setup() {
  20. // filename for this example
  21. char name[] = "APPEND.TXT";
  22. Serial.begin(9600);
  23. while (!Serial) {} // wait for Leonardo
  24. // pstr() stores strings in flash to save RAM
  25. cout << endl << pstr("Type any character to start\n");
  26. while (Serial.read() <= 0) {}
  27. delay(400); // Catch Due reset problem
  28. // initialize the SD card at SPI_HALF_SPEED to avoid bus errors with
  29. // breadboards. use SPI_FULL_SPEED for better performance.
  30. if (!sd.begin(chipSelect, SPI_HALF_SPEED)) sd.initErrorHalt();
  31. cout << pstr("Appending to: ") << name;
  32. for (uint8_t i = 0; i < 100; i++) {
  33. // open stream for append
  34. ofstream sdout(name, ios::out | ios::app);
  35. if (!sdout) error("open failed");
  36. // append 100 lines to the file
  37. for (uint8_t j = 0; j < 100; j++) {
  38. // use int() so byte will print as decimal number
  39. sdout << "line " << int(j) << " of pass " << int(i);
  40. sdout << " millis = " << millis() << endl;
  41. }
  42. // close the stream
  43. sdout.close();
  44. if (!sdout) error("append data failed");
  45. // output progress indicator
  46. if (i % 25 == 0) cout << endl;
  47. cout << '.';
  48. }
  49. cout << endl << "Done" << endl;
  50. }
  51. //------------------------------------------------------------------------------
  52. void loop() {}