How to read text file line by line?

What function can arduino UNO read each line in text file on a sd card?

See http://arduino.cc/en/Reference/SD#.UxMCvc51O8U To read line by line you have to loop reading into a buffer till a newline is encountered. Also check for buffer overflow round the loop in case lines are too long.

MarkT: See http://arduino.cc/en/Reference/SD#.UxMCvc51O8U To read line by line you have to loop reading into a buffer till a newline is encountered. Also check for buffer overflow round the loop in case lines are too long.

Do you have a script example? If you have . would you share it for me,?

You can read a text file line by line using the readBytesUntil() function as per this example:

void LoadAlarmTimes() {				
  //Initialise the array to Nulls/Zeros/False				
  for (byte l_idx = 0; l_idx < DC_AlarmTimerCount; l_idx++) {			
    G_AlarmTimerList[l_idx].DayOfWeek = "NULL";		
    G_AlarmTimerList[l_idx].StartTime = 0;		
    G_AlarmTimerList[l_idx].StartActioned = false;		
    G_AlarmTimerList[l_idx].EndTime = 0;		
    G_AlarmTimerList[l_idx].EndActioned = false;		
  }		
  //Read file line by line and load into the array				
  String l_filename = EPSR(E_ALRMTIMEdtTXT_2533);			
  if (!SD.exists(&l_filename[0])) {			
    //Serial.println("File NOT FOUND");
    return;	
  }
  //Serial.println("File FOUND");
  File l_file = SD.open(&l_filename[0],FILE_READ);			
  if (!l_file) {
    return;
  }
  //Serial.println("File open");
  String l_line;	
  String l_field;		
  int l_idx = 0;		
  int l_posn;	
  while (l_file.available() != 0) {			
    l_line = l_file.readStringUntil('\n');		
    l_line.trim();		
    l_posn = 0;
    //Serial.println(l_line);
    l_field = ENDF2(l_line,l_posn,'\t');		
    if ((l_field == "MON") || (l_field == "TUE") || (l_field == "WED") || (l_field == "THU") || (l_field == "FRI") || (l_field == "SAT") || (l_field == "SUN")) {		
      //Serial.println(l_line);
      G_AlarmTimerList[l_idx].DayOfWeek = l_field;	
      l_field = ENDF2(l_line,l_posn,'\t');	
      if (l_field != "")
        G_AlarmTimerList[l_idx].StartTime = TimeExtract(l_field);
      //
      l_field = ENDF2(l_line,l_posn,'\t');	
      if (l_field != "")
        G_AlarmTimerList[l_idx].EndTime = TimeExtract(l_field);
      //
      l_idx++;		
    }		
    if (l_idx == DC_AlarmTimerCount) {
    l_file.close();			
      return;
    }		
  }			
  l_file.close();			
}

However reading a text file line by line into a String and then parsing the line/String to extract fields can do nasty things to your application memory by creating a lot of wasted free heap memory space that is not released until you exit from your procedure (also assuming you are not updating global strings within your text file read procedure). This will likely be a bigger problem for text files with large numbers of lines to be processed.

However if the lines of your text file are short and typically of the same length you may not have much of a problem. Also investigate the String.reserve() function.

The following procedure avoids some String memory problems by avoiding chopping processed fields off the front of your input line and fragmenting heap memory:

String ENDF2(String &p_line, int &p_start, char p_delimiter) {
//Extract fields from a line one at a time based on a delimiter.
//Because the line remains intact we dont fragment heap memory
//p_start would normally start as 0
//p_start increments as we more along the line
//We return p_start = -1 with the last field

  //If we have already parsed the whole line then return null
  if (p_start == -1) {
	return "";
  }

  int l_start = p_start;
  int l_index = p_line.indexOf(p_delimiter,l_start);
  if (l_index == -1) { //last field of the data line
	p_start = l_index;
	return p_line.substring(l_start);
  }
  else { //take the next field off the data line
	p_start = l_index + 1;
	return p_line.substring(l_start,l_index); //Include, Exclude
  }
}

I use the above code to load about twelve lines of alarm configuration data into an in-memory array structure and have no problems. You will note that my text file uses tab delimited records but it could use any delimiter.

I hope this helps.

Cheers

Catweazle NZ

CatweazleNZ, Thank you very much.