Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

pirms 10 gadiem
pirms 10 gadiem
pirms 10 gadiem
pirms 10 gadiem
pirms 10 gadiem
pirms 10 gadiem
pirms 10 gadiem
pirms 10 gadiem
pirms 10 gadiem
pirms 10 gadiem
pirms 10 gadiem
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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.read() <= 0) {}
  62. delay(400); // catch Due reset problem
  63. // initialize the SD card at SPI_HALF_SPEED to avoid bus errors with
  64. // breadboards. use SPI_FULL_SPEED for better performance.
  65. if (!sd.begin(chipSelect, SPI_HALF_SPEED)) {
  66. sd.initErrorHalt();
  67. }
  68. makeTestFile();
  69. demoFgets();
  70. cout << F("\nDone\n");
  71. }
  72. void loop(void) {}