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

10 vuotta sitten
10 vuotta sitten
10 vuotta sitten
10 vuotta sitten
10 vuotta sitten
10 vuotta sitten
10 vuotta sitten
10 vuotta sitten
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. * Append Example
  3. *
  4. * This program shows how to use open for append.
  5. * The program will append 100 line each time it opens the file.
  6. * The program 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. // Wait for USB Serial
  24. while (!Serial) {
  25. SysCall::yield();
  26. }
  27. // F() stores strings in flash to save RAM
  28. cout << endl << F("Type any character to start\n");
  29. while (Serial.read() <= 0) {
  30. SysCall::yield();
  31. }
  32. // initialize the SD card at SPI_HALF_SPEED to avoid bus errors with
  33. // breadboards. use SPI_FULL_SPEED for better performance.
  34. if (!sd.begin(chipSelect, SPI_HALF_SPEED)) {
  35. sd.initErrorHalt();
  36. }
  37. cout << F("Appending to: ") << name;
  38. for (uint8_t i = 0; i < 100; i++) {
  39. // open stream for append
  40. ofstream sdout(name, ios::out | ios::app);
  41. if (!sdout) {
  42. error("open failed");
  43. }
  44. // append 100 lines to the file
  45. for (uint8_t j = 0; j < 100; j++) {
  46. // use int() so byte will print as decimal number
  47. sdout << "line " << int(j) << " of pass " << int(i);
  48. sdout << " millis = " << millis() << endl;
  49. }
  50. // close the stream
  51. sdout.close();
  52. if (!sdout) {
  53. error("append data failed");
  54. }
  55. // output progress indicator
  56. if (i % 25 == 0) {
  57. cout << endl;
  58. }
  59. cout << '.';
  60. }
  61. cout << endl << "Done" << endl;
  62. }
  63. //------------------------------------------------------------------------------
  64. void loop() {}