No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

77 líneas
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. // 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.available()) {
  30. SysCall::yield();
  31. }
  32. // Initialize at the highest speed supported by the board that is
  33. // not over 50 MHz. Try a lower speed if SPI errors occur.
  34. if (!sd.begin(chipSelect, SD_SCK_MHZ(50))) {
  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() {}