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.

82 líneas
2.1KB

  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. while (!Serial) {} // Wait for Leonardo
  57. cout << F("Type any character to start\n");
  58. while (Serial.read() <= 0) {}
  59. delay(400); // catch Due reset problem
  60. // initialize the SD card at SPI_HALF_SPEED to avoid bus errors with
  61. // breadboards. use SPI_FULL_SPEED for better performance.
  62. if (!sd.begin(chipSelect, SPI_HALF_SPEED)) {
  63. sd.initErrorHalt();
  64. }
  65. makeTestFile();
  66. demoFgets();
  67. cout << F("\nDone\n");
  68. }
  69. void loop(void) {}