-1

So I have a programme that loads a text file from an sd card into a string array. Each line in the text file is stored in a different string in a string array. Well its supposed to be.

file = SD.open("/classes/basic.txt", FILE_READ);
Serial.print("Reading, Checking and Compiling \n\n\n");  

for(int i = 0; i < sizeof(result)-1;i++){
 while(file.available()) {
  character23 = file.read();
  content.concat(character23); 

}    
if (content != "") {     
  result[i] = content;
    Serial.println(content);

}

}

I want each line in the txt file to be stored in a different string in the array, in chronological order and each string has to be accessible individually. The problem with this code is that it stores the whole text file in one string. How can I fix this?

EDIT: here is what I have now

  file = SD.open("/classes/basic.txt", FILE_READ);
  Serial.print("Reading, Checking and Compiling \n\n\n");  

 for(int i = 0; i < sizeof(result)-1;i++){
   while(file.available()) {
  character23 = file.read();
  if (character23 == '^') break;
  content.concat(character23); 

}    
if (content != "") {     
  result[i] = content;
  Serial.println(content);

  delay(1000);
}

} Serial.println(result[3]);

1 Answer 1

1

To do that, you need to read the file until you reach a line break.

Replace this code:

while(file.available()) {
  character23 = file.read();
  content.concat(character23);
}

by this:

while(file.available()) {
   character23 = file.read();
   if (character23 == '\n') break;
   content.concat(character23);
}
12
  • I'm trying to debug and I'm trying to find what is stored in a certain string of the array is this correct: Serial.println(result[1]); because it keeps returning nothing Commented Feb 16, 2016 at 21:16
  • 1
    Can you show your complete code rather than a snippet? Commented Feb 16, 2016 at 21:17
  • ye ill edit the above code Commented Feb 16, 2016 at 21:19
  • where is the declaration of the array? Also, this line is wrong if (character23 == '^') break;. It should be \n not ^. Commented Feb 16, 2016 at 21:22
  • ye but all the lines in the txt file end in ^ Commented Feb 16, 2016 at 21:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.