Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

append.ino 1.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. while (!Serial) {} // wait for Leonardo
  24. // F() stores strings in flash to save RAM
  25. cout << endl << F("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)) {
  31. sd.initErrorHalt();
  32. }
  33. cout << F("Appending to: ") << name;
  34. for (uint8_t i = 0; i < 100; i++) {
  35. // open stream for append
  36. ofstream sdout(name, ios::out | ios::app);
  37. if (!sdout) {
  38. error("open failed");
  39. }
  40. // append 100 lines to the file
  41. for (uint8_t j = 0; j < 100; j++) {
  42. // use int() so byte will print as decimal number
  43. sdout << "line " << int(j) << " of pass " << int(i);
  44. sdout << " millis = " << millis() << endl;
  45. }
  46. // close the stream
  47. sdout.close();
  48. if (!sdout) {
  49. error("append data failed");
  50. }
  51. // output progress indicator
  52. if (i % 25 == 0) {
  53. cout << endl;
  54. }
  55. cout << '.';
  56. }
  57. cout << endl << "Done" << endl;
  58. }
  59. //------------------------------------------------------------------------------
  60. void loop() {}