Using multiple SD files, do I need to declare more than 1 or use the same one?

Hello,
I want to use a subroutine that can read alternate files from the SD card. If I delcare 2 FIles such as File phoneNumFile, and File fileTwo, and have them both open at the same time, I would like to use a function to do the same process alternatly. How do you pass a File type to the subroutine? I have my code following.

char phoneNumbers[100];
char authNumbers[100];
File phoneNumFile;
File authNumFile;
 void getPhoneNumbers(phoneNumbers,"phoneNum.txt", phoneNumFile)
 void getPhoneNumbers(authNumbers,"authNum.txt", authNumFile)


void getPhoneNumbers(char anyPhoneNumbers[100], char nameFile[12] ="", File = anyFile){
  memset (anyPhoneNumbers,'\0',100);
  int charPos = 0;
  char thisChar;

  anyFile = SD.open("nameFile", FILE_READ);
      if (! anyFile) {
    Serial.println("error opening nameFile.txt");
    // Wait forever since we cant read data
    while (1) ;
  } 
  if (anyFile) {
    while (anyFile.available()) {
      thisChar = anyFile.read();
      //READ PHONE NUMBERS AS A STRING TO BUFFER TO PARSE LATER
      if(charPos < 99) {
        anyPhoneNumbers[charPos] = thisChar;
        charPos++;
        anyPhoneNumbers[charPos] = 0;
      }  
    }
    charPos++;       
    anyPhoneNumbers[charPos] = '\r';
    Serial.println(charPos,DEC);
    charPos++;
    anyPhoneNumbers[charPos] = '\n';
    //      Serial.write(thisChar);
    anyFile.close();
  } 
  // if the file isn't open, pop up an error:
  else {
    Serial.println("error opening nameFile.txt");
  }
}

This code does what I want but I am trying to make it into a subroutine. I don't seem to have handle on this. I can just do it in line and it works just fine but it would be nice to be able to call the routine and pass it the buffer name, file name and the File associated with filename. I can't get this code to compile. The subroutine comples but not the calling line, Any suggestions?

Stank

There are so much errors in this code. You should read some C tutorials about functions, pointers..

hello again,

I could use pointers but I didn't, I used character arrays and it works as in line code. I just repeat the code and change the names of the File, the file name and the buffer. My question is regarding the SD card, having multiple files open and how to pass the parameters for each file to the subroutine. I can't find any good examples or information on this particular situation. Maybe someone might know where I could look.

StanK

Well, the files will never be open simultaneously because you call close() at the end of your function, so... you could use a single file handler and have the same result. I think you forgot to post the original, working code so we can't see differences

Hi guix,

I am sorry for the trouble. I think I was having a brain fart. I got it to work. I think what I don't understand is the the relationship of the FILE declaration and file access. So each file that is open needs a file handler, i.e File phoneNumFile; or File authNumFile; but I don't need one for each file as long as I close each one before opening another. Am I correct? I'll go do some reading. I don't know enough about file access.

Thanks for the reply,

StanK

StanK:
I don't need one for each file as long as I close each one before opening another. Am I correct?

Yes correct :wink: