I created a text file (string.txt) with the following format:
string1\n
\n
string2\n
\n
...
I also wrote a function which returns the i'th string in the text file (it does so by counting the endlines). Here is the stub:
char* getString(char file[], int i);
The problem is that I can't make consecutive, multiple calls to getString() without the file.open() returning "false". For instance:
getString("string.txt", 1); // This is ok.
but
getString("string.txt", 1);
getString("string.txt", 2); //This causes open() to return false.
I worked under the assumption that after closing a file, reopenning the same file causes the byte pointer (from which read() pulls a byte) to point to the very beginning (and not point at the last thing that it pointed to before closing.
Attached my code in case you wanted to try it out.
/*
* Note to Self:
* 1. The formal parameter of an arduino function should be a char[] if you wish to pass a string in "" (quotes form)
into the function.
2. strcpy must be passed a "" (empty string in quotes) to "clear" the string; Entering anything into
strcpy(quote, "<breaks program>");
will break the program.
3. char* quote = (char*)malloc(sizeof(char)*800);
Mallocing is hard coded by longest quote we've in .txt file (we found upper bound).
We need a method to find the size in bytes of the quote. Also, it seems that we can't physically
appropriate more than ~800+ bytes for characters. The program will store the string until it can't,
crashes, and then reboots. This limits how big of a quote we can have!
4. Remember to free() after malloc()!
*/
#include <string.h>
#include <SPI.h>
#include <SD.h>
#define DEBUG 0
char *getQuote(char txtFile[], int quoteIndex){
char defaultString[] = "Error: Could not retrieve quote.";
char* quote = (char*)malloc(sizeof(char)*800);
strcpy(quote, "");
int preQuoteIndex = quoteIndex*2; //In the text, 2 endlines flag the quote they precede
File file1;
file1 = SD.open(txtFile); //Open file for reading
if(file1){
#if DEBUG
while (file1.available()){
Serial.write(file1.read());
}
#endif
//Formula: (quoteIndex*2)-2 gives the number of endlines to check for before actually storing what is read
int i=0; //Use by read() to find where in the text file we should begin reading the quote
while((i <= preQuoteIndex-2) && (i != preQuoteIndex-2)){ //Move the reader cursor to the start of the quote
if(file1.read() == '\n')
i++;
}
//Store the quote for return
strcpy(quote, ""); //Clear the string
char newLetter[2];
newLetter[1] = '\0';
while(file1.peek() != '\n'){
newLetter[0] = file1.read(); //Generate the character string
strcat(quote, newLetter); //Append the next letter
}
Serial.println(quote); //DEBUG
free(quote);
file1.close();
}else{ Serial.println("Error opening .txt for reading.");}
return quote;
}
void setup() {
Serial.begin(9600);
//INITIATE SD
//pinMode(10, OUTPUT); //SPI Requirement -- NOT NEEDED FOR SOME REASON?INCLUSION CAUSES CRASH
if(SD.begin(4) == true){Serial.println("SD init success"); Serial.println();}
else if(SD.begin(4) == false){Serial.println("SD init failure"); Serial.println();}
//Call getQuote("U_Quotes.txt", index) to get a quote for display
getQuote("U_Quotes.txt", 1);
getQuote("U_Quotes.txt", 2); //Comment this out or program fails!
//getQuote("U_Quotes.txt", 3);
}
void loop() {
}
U_QUOTES.txt (5.3 KB)