Open file read from a list

It’s late, I can’t think straight … I have a text file that contains a list of filenames, one per line:

file-1.txt
filea-02.txt
files1.txt
filename.txt
etc.

Call it a control file if you will. It lists the names of files I need to open at a later stage, one at a time. Basically, read the first filename, go open and read data from it. When done, close it, and go read the next file on the list from the control file. Lather, rinse, repeat.

How do I read a full line, and pass that on to .open(, O_READ) ?

How do I read a full line

First, you need to understand that the file does not contain "lines". It contains a stream of data. There are some characters in that stream that cause applications to do things like return to the beginning of the line (carriage return) or move to the next line (line feed).

Thus, you need to define exactly what constitutes a "record", as there is nothing intrinsic that defines this. Typically, a carriage return/line feed combo marks the end of a "record".

Then, you read all the characters in the file, one at a time, storing them in an array, followed by a NULL, until you encounter the one(s) that mark the end of a "record". When you find the one(s) that mark the end of a record, you have a file name in the array, and can pass that array to the SD.open() function.

The easiest way to read a line from a text file with SdFat is to use fgets(). See the fgets example in the SdFat/examples folder.

You will need to zero the ‘\n’ delimiter that is returned like this:

  // Room for 12 byte file name, '\n' , and zero byte.
  char line[14];

  // Read line and delimiter.
  int n = rdfile.fgets(line, sizeof(line));

  // Check line.
  if (n <= 0) {
    // Error if -1, EOF if zero.
  }

  // Zero '\n'.  Last line in file may not have end line.  Also, no '\n' if line is too long.
  if (line[n-1] == '\n') line[n-1] = 0;

Here is detailed documentation for fgets.

int16_t SdBaseFile::fgets ( char * str,
int16_t num,
char * delim = 0
)

Get a string from a file.

fgets() reads bytes from a file into the array pointed to by str, until num - 1 bytes are read, or a delimiter is read and transferred to str, or end-of-file is encountered. The string is then terminated with a null byte.

fgets() deletes CR, ‘\r’, from the string. This insures only a ‘\n’ terminates the string for Windows text files which use CRLF for newline.

Parameters:
[out] str Pointer to the array where the string is stored.
[in] num Maximum number of characters to be read (including the final null byte). Usually the length of the array str is used.
[in] delim Optional set of delimiters. The default is “\n”.

Returns:
For success fgets() returns the length of the string in str. If no data is read, fgets() returns zero for EOF or -1 if an error occurred.

I'm at the office so I can't test it, but, I'm assuming I then pass 'line' directly to .open() ?

I’m assuming I then pass ‘line’ directly to .open() ?

Yes, if each line contains only the file name.

fat16lib:

I'm assuming I then pass 'line' directly to .open() ?

Yes, if each line contains only the file name.

They will be, I'm the one creating that file. Though I'm still trying to decide if I want to add other parameters to that file (in which case each line will have markers separating the parameters), or do something else. Still playing with it ...

I'm still pondering moving away from pre-processing image files and using 24-bit BMP files. That would be the only reason to add parameters to the control file as the BMP file contains the other necessary information I need such as width and height. Meh, we'll see.