Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Demo of fgets function to read lines from a file.
  2. #include <SPI.h>
  3. #include "SdFat.h"
  4. // SD chip select pin
  5. const uint8_t chipSelect = SS;
  6. SdFat sd;
  7. // print stream
  8. ArduinoOutStream cout(Serial);
  9. //------------------------------------------------------------------------------
  10. // store error strings in flash memory
  11. #define error(s) sd.errorHalt(F(s))
  12. //------------------------------------------------------------------------------
  13. void demoFgets() {
  14. char line[25];
  15. int n;
  16. // open test file
  17. SdFile rdfile("fgets.txt", O_READ);
  18. // check for open error
  19. if (!rdfile.isOpen()) {
  20. error("demoFgets");
  21. }
  22. cout << endl << F(
  23. "Lines with '>' end with a '\\n' character\n"
  24. "Lines with '#' do not end with a '\\n' character\n"
  25. "\n");
  26. // read lines from the file
  27. while ((n = rdfile.fgets(line, sizeof(line))) > 0) {
  28. if (line[n - 1] == '\n') {
  29. cout << '>' << line;
  30. } else {
  31. cout << '#' << line << endl;
  32. }
  33. }
  34. }
  35. //------------------------------------------------------------------------------
  36. void makeTestFile() {
  37. // create or open test file
  38. SdFile wrfile("fgets.txt", O_WRITE | O_CREAT | O_TRUNC);
  39. // check for open error
  40. if (!wrfile.isOpen()) {
  41. error("MakeTestFile");
  42. }
  43. // write test file
  44. wrfile.print(F(
  45. "Line with CRLF\r\n"
  46. "Line with only LF\n"
  47. "Long line that will require an extra read\n"
  48. "\n" // empty line
  49. "Line at EOF without NL"
  50. ));
  51. wrfile.close();
  52. }
  53. //------------------------------------------------------------------------------
  54. void setup(void) {
  55. Serial.begin(9600);
  56. // Wait for USB Serial
  57. while (!Serial) {
  58. SysCall::yield();
  59. }
  60. cout << F("Type any character to start\n");
  61. while (!Serial.available()) {
  62. SysCall::yield();
  63. }
  64. delay(400); // catch Due reset problem
  65. // Initialize at the highest speed supported by the board that is
  66. // not over 50 MHz. Try a lower speed if SPI errors occur.
  67. if (!sd.begin(chipSelect, SD_SCK_MHZ(50))) {
  68. sd.initErrorHalt();
  69. }
  70. makeTestFile();
  71. demoFgets();
  72. cout << F("\nDone\n");
  73. }
  74. void loop(void) {}