SD library; declaring different file names using "if" statment

Status
Not open for further replies.

instrumentek

Well-known member
Hi;
I'm wondering if there is a way to call different file names to one file declaration so I can save on repeated coding for different file names. here is an example of my code:

Code:
#include <SD.h>
#include <SPI.h>

//________________________________________________
void setup()
{
  Serial.begin(9600);
  SD.begin();
}
//________________________________________________
void loop()
{
  fileDump(1);
  fileDump(2);
  fileDump(3);
}
//________________________________________________
int fileDump(int fileNumber)
{
  if (fileNumber == 1)
    File myFile = SD.open("test1.txt");

  if (fileNumber == 2)
    File myFile = SD.open("test2.txt");

  if (fileNumber == 3)
    File myFile = SD.open("test3.txt");

 //_________________________________do file stuff
  while (myFile.available())
  {
    Serial.write(myFile.read());
  }
  myFile.close();
}

I get the following error code "'myFile' was not declared in this scope". I have tried declaring "File myFile" before the if statements and it complies but fails to load the file

________________________________________________________

- Teensy 3.1
- Arduino 1.63
- Teensy loader 1.25

Thanks for any advice
 
The variable myFile should be declared only once. You were on the right track when you had "File myFile" before the if statements. For a situation like this, I'd put the file names in an array. I haven't tried compiling this.

Code:
int fileDump(int fileNumber) {
  const char* fileNames = { "test1.txt", "test2.txt", "test3.txt" };
  if (1 <= fileNumber && fileNumber <= 3) {
    File myFile = SD.open(fileNames[fileNumber]);
    while (myFile.available()) {
      Serial.write(myFile.read());
    }
    myFile.close();
  } else {
    Serial.write("Error: invalid file number. Expecting 1, 2 or 3.");
  }
}
 
Or try this:
Code:
void fileDump(int fileNumber)
{
  char fname[16];

  sprintf(fname,"test%d.txt",fileNumber);
  File myFile = SD.open(fname);
  if(myFile == NULL) {
    Serial.print("ERROR: can't open ");
    Serial.println(fname);
    return;
  }
 //_________________________________do file stuff
  while (myFile.available())
  {
    Serial.write(myFile.read());
  }
  myFile.close();
}
Note that I declared fileDump to return void because it doesn't return anything. I also print an error message if for some reason the file can't be opened.

Pete
 
Hi;

I tried both the first and second method listed above:

The first method gives me an error: first - invalid conversion from 'char' to 'const char*' [-fpermissive]

The second method works great thanks! However there is one minor issue. I used file names "test1,test2,test3" as examples I would prefer if i could use there proper file names. however in this second method I need to use a similar file name with a different number in it.

I can use the second method but it would be better if something similar to the first method would compile. Do you have any suggestions on how to modify the first example so it would compile?
 
Maybe try changing this:

Code:
  const char* fileNames = { "test1.txt", "test2.txt", "test3.txt" };

to this:

Code:
  const char* fileNames[] = { "test1.txt", "test2.txt", "test3.txt" };


Or in the original code, try changing this:

Code:
  if (fileNumber == 1)
    File myFile = SD.open("test1.txt");

  if (fileNumber == 2)
    File myFile = SD.open("test2.txt");

  if (fileNumber == 3)
    File myFile = SD.open("test3.txt");

to this:

Code:
  File myFile;

  if (fileNumber == 1)
    myFile = SD.open("test1.txt");

  if (fileNumber == 2)
    myFile = SD.open("test2.txt");

  if (fileNumber == 3)
    myFile = SD.open("test3.txt");

Notice this fix involves declaring "File myFile" before the 3 if statements, AND not declaring "File" inside each of the if statements.
 
Status
Not open for further replies.
Back
Top