1

Hello I have an Arduino program that loads strings from a file and stores them in an array. It works fine when I load it the first time but when I load it a second time it doesn't seem to work.

 String content = "";

 char character23;

 String result[10];
 int arraysize = 10;

 void loadPrograme(String sss){
   File file = SD.open("basic.txt", FILE_READ);
   Serial.print("Reading, Checking and Compiling \n\n\n");  

   for (int i = 0; i < arraysize; i++) { 
     result[i] = "";
   }

   for (int i = 0; i < arraysize; i++) {
     while (file.available()) {
       character23 = file.read(); 
       if (character23 == '\n') break; 
       if (character23 != '^') { 
         content += character23; 
       } 
     } 
     content.trim(); 
     if (content != "") { 
       result[i] = content; 
       content = "";
     } 
   } 

   for (int i = 0; i <arraysize; i++) { 
     Serial.print("result["); 
     Serial.print(i); 
     Serial.print("]: "); 
     Serial.println(result[i]); 
   }
   file.close();
 }

This works fine when I call loadProgramme("basic.txt") in the setup function.

void loadfileMethod(String s){
  if(consolemode){
    u8g.setFont(u8g_font_micro);
    u8g.drawStr(2, 6-camy, s.c_str());
    camy+=6;
  }
  camy = 0;
  loadPrograme("names.txt");
}   

When I call loadprograme in this method it runs through loadrograme but only gets as far as the while loop and it doesn't do anything in the while loop.(I check this using serial.println().

Can someone tell me what I'm doing wrong that makes it work the first time and not the second?

EDIT: I found the problem but I don't know how to fix it. when I do Serial.println(file.name()); it prints the correct name the first time but the second time it just prints a tiny box which I presume is nothing or an error.

How can I fix this?

  • how many bytes are all these strings your loading going to take up? – BrettAM Feb 19 '16 at 16:21
  • they don't take up that much – user2279603 Feb 19 '16 at 16:26
  • no matter the size of the file, it dosnt work – user2279603 Feb 19 '16 at 16:28
  • I have found the problem but I don't know why it happens or how to fix it. – user2279603 Feb 19 '16 at 16:29
  • Please add the code for the declarations of the variables you are having issues with. – Dave X Feb 19 '16 at 16:50
0

If you know that your while loop isn't working as expected, I'd instrument the while loop and see if it parses as you expect:

for (int i = 0; i < arraysize; i++) {
  while (file.available()) {
    character23 = file.read(); 
    if (character23 == '\n') break; 
    if (character23 != '^') { 
      content += character23; 
    }
    Serial.print(i);
    Serial.print(':');
    Serial.print(character23);
    Serial.print(':');
    Serial.println(content); 
  } 
  content.trim(); 
  if (content != "") { 
    result[i] = content; 
    content = "";
  } 
} 

Are you certain it is working the first time?

  • yes it does work the first time – user2279603 Feb 19 '16 at 22:49
  • I thought perhaps it might be reading the entire input file in the first cycle, leaving nothing for the rest of the results. Perhaps there are a number of '\n' characters, or nulls that might be confusing things. If your Serial.println(file.name()) fails in the second iteration of the while, then perhaps the file is no longer open? – Dave X Feb 21 '16 at 4:01

Not the answer you're looking for? Browse other questions tagged or ask your own question.