Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

78 lines
1.8KB

  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. #include "sdios.h"
  11. // SD chip select pin
  12. const uint8_t chipSelect = SS;
  13. // file system object
  14. SdFat sd;
  15. // create Serial stream
  16. ArduinoOutStream cout(Serial);
  17. // store error strings in flash to save RAM
  18. #define error(s) sd.errorHalt(F(s))
  19. //------------------------------------------------------------------------------
  20. void setup() {
  21. // filename for this example
  22. char name[] = "append.txt";
  23. Serial.begin(9600);
  24. // Wait for USB Serial
  25. while (!Serial) {
  26. SysCall::yield();
  27. }
  28. // F() stores strings in flash to save RAM
  29. cout << endl << F("Type any character to start\n");
  30. while (!Serial.available()) {
  31. SysCall::yield();
  32. }
  33. // Initialize at the highest speed supported by the board that is
  34. // not over 50 MHz. Try a lower speed if SPI errors occur.
  35. if (!sd.begin(chipSelect, SD_SCK_MHZ(50))) {
  36. sd.initErrorHalt();
  37. }
  38. cout << F("Appending to: ") << name;
  39. for (uint8_t i = 0; i < 100; i++) {
  40. // open stream for append
  41. ofstream sdout(name, ios::out | ios::app);
  42. if (!sdout) {
  43. error("open failed");
  44. }
  45. // append 100 lines to the file
  46. for (uint8_t j = 0; j < 100; j++) {
  47. // use int() so byte will print as decimal number
  48. sdout << "line " << int(j) << " of pass " << int(i);
  49. sdout << " millis = " << millis() << endl;
  50. }
  51. // close the stream
  52. sdout.close();
  53. if (!sdout) {
  54. error("append data failed");
  55. }
  56. // output progress indicator
  57. if (i % 25 == 0) {
  58. cout << endl;
  59. }
  60. cout << '.';
  61. }
  62. cout << endl << "Done" << endl;
  63. }
  64. //------------------------------------------------------------------------------
  65. void loop() {}