You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * This example reads a simple CSV, comma-separated values, file.
  3. * Each line of the file has three values, a long and two floats.
  4. */
  5. #include <SdFat.h>
  6. // SD chip select pin
  7. const uint8_t chipSelect = SS;
  8. // file system object
  9. SdFat sd;
  10. // create Serial stream
  11. ArduinoOutStream cout(Serial);
  12. char fileName[] = "TESTFILE.CSV";
  13. //------------------------------------------------------------------------------
  14. // store error strings in flash to save RAM
  15. #define error(s) sd.errorHalt_P(PSTR(s))
  16. //------------------------------------------------------------------------------
  17. // read and print CSV test file
  18. void readFile() {
  19. long lg;
  20. float f1, f2;
  21. char text[10];
  22. char c1, c2, c3; // space for commas.
  23. // open input file
  24. ifstream sdin(fileName);
  25. // check for open error
  26. if (!sdin.is_open()) error("open");
  27. // read until input fails
  28. while (1) {
  29. // Get text field.
  30. sdin.get(text, sizeof(text), ',');
  31. // Assume EOF if fail.
  32. if (sdin.fail()) break;
  33. // Get commas and numbers.
  34. sdin >> c1 >> lg >> c2 >> f1 >> c3 >> f2;
  35. // Skip CR/LF.
  36. sdin.skipWhite();
  37. if (sdin.fail()) error("bad input");
  38. // error in line if not commas
  39. if (c1 != ',' || c2 != ',' || c3 != ',') error("comma");
  40. // print in six character wide columns
  41. cout << text << setw(6) << lg << setw(6) << f1 << setw(6) << f2 << endl;
  42. }
  43. // Error in an input line if file is not at EOF.
  44. if (!sdin.eof()) error("readFile");
  45. }
  46. //------------------------------------------------------------------------------
  47. // write test file
  48. void writeFile() {
  49. // create or open and truncate output file
  50. ofstream sdout(fileName);
  51. // write file from string stored in flash
  52. sdout << pstr(
  53. "Line 1,1,2.3,4.5\n"
  54. "Line 2,6,7.8,9.0\n"
  55. "Line 3,9,8.7,6.5\n"
  56. "Line 4,-4,-3.2,-1\n") << flush;
  57. // check for any errors
  58. if (!sdout) error("writeFile");
  59. sdout.close();
  60. }
  61. //------------------------------------------------------------------------------
  62. void setup() {
  63. Serial.begin(9600);
  64. while (!Serial) {} // wait for Leonardo
  65. cout << pstr("Type any character to start\n");
  66. while (Serial.read() <= 0) {}
  67. delay(400); // catch Due reset problem
  68. // initialize the SD card at SPI_HALF_SPEED to avoid bus errors with
  69. // breadboards. use SPI_FULL_SPEED for better performance
  70. if (!sd.begin(chipSelect, SPI_HALF_SPEED)) sd.initErrorHalt();
  71. // create test file
  72. writeFile();
  73. cout << endl;
  74. // read and print test
  75. readFile();
  76. cout << "\nDone!" << endl;
  77. }
  78. void loop() {}