From cf36412e51fe68850ce05559f205e37d25257c57 Mon Sep 17 00:00:00 2001 From: Jim Martens Date: Sat, 4 Jul 2015 12:29:57 +0200 Subject: [PATCH] [ES] Added readString method MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-óff-by: Jim Martens --- es/blatt6/uebung6-1/uebung6-1.ino | 33 +++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/es/blatt6/uebung6-1/uebung6-1.ino b/es/blatt6/uebung6-1/uebung6-1.ino index 8a1ecf2..923e8c7 100644 --- a/es/blatt6/uebung6-1/uebung6-1.ino +++ b/es/blatt6/uebung6-1/uebung6-1.ino @@ -1,4 +1,5 @@ #include +#include // pins int slaveSelectPin = 10; @@ -304,3 +305,35 @@ int printString(int x, int y, const char* text, int textLength) return 0; } + +/** + * Reads the contents of a text file into a string. + * + * Returns an empty string on failure. + * + * @param File file + */ +const char* readString(File file) +{ + if (file) { + unsigned long fileSize = file.size(); + char result[fileSize]; + int i = 0; + while (file.available()) { + char c = file.read(); + if (c != '\n') { + result[i] = c; + i++; + } + else { + result[i] = '\0'; + } + } + + return result; + } + else { + // something happened + return ""; + } +}