Problem reading data from SD card and use it to connect on WiFi

Hey guys i’ve been struggling with a problem and i hope someone can help me out.

I just created a text file on a sd card containing 2 strings that will be my SSID and Pass to connect on a WiFi network using the arduino WiFi shield, and i’ve been trying to read them from the file and use them to connect but, everytime i try i get the error led on. I’ve tried declaring the strings in the code directly and it works, but reading them from the file i can’t get a connection, even thou i debugged a while and the strings i get from the file are printing as intended on the Serial output.

Any help would be much appreciated.
Obs: I structed the code to read the file being the first line of the file the SSID and the second line the Pass.
Using 1.02 IDE

EDIT: Sorry guys i found out the problem, i was not checking for a carriage return (’\r’).
I managed to get it working, dont need to reply! Thanks anyway :wink:

Code:

 #include <SPI.h>
#include <WiFi.h>
#include <SD.h>

File myFile;
const char CONF[]  = "conf.txt";
int status = WL_IDLE_STATUS;
const int i1 = 1;
const int i0 = 0;
int i = i0;
const int tenDelay = 10000;


void setup(){

  Serial.begin(9600);
  initialize();
}

void loop(){

}

void initialize(){
  loadConf();


}

void connectWifi(char ssid[], char pass[]){
  // check for the presence of the shield:
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println(F("WiFi shield not present")); 
    while(true);        // don't continue
  } 

  // attempt to connect to Wifi network:
  while (status != WL_CONNECTED) { 
    Serial.print(F("Attempting to connect to Network named: "));
    Serial.println(ssid);                   // print the network name (SSID);

    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:    
    status = WiFi.begin(ssid, pass);
    // wait 10 seconds for connection:
    delay(tenDelay);
  } 
}

void loadConf(){

  if (!SD.begin(4)) {
    Serial.println(F("initialization sd failed!"));
    return;
  }
  Serial.println(F("iniialization sd done."));

  int ssidLength = 0;
  int passLength = 0;
  // open the file for measure the string's length
  myFile = SD.open(CONF);
  if (myFile) {
    i = i0;
    while (myFile.available()) {
      char c = myFile.read();
      if(i==i0){
        if(c == '\n'){
          i = i1;        
        }
        else{
          ssidLength++;
        }
      }
      else{
        if(c != '\r'){
          passLength++;
        }
      }
    }
    // close the file:
    myFile.close();
  } 

  char ssid[ssidLength+1];
  char pass[passLength+1];

  // re-open the file for reading the strings
  myFile = SD.open(CONF);
  if (myFile) {
    i = i0;
    int j = i0;

    while (myFile.available()) {
      char c = myFile.read();
      if(i==i0){
        if(c == '\n'){
          i = i1;
          //Closing the string
          ssid[j++] = '\0';
          j = i0;          
        }
        else{
          ssid[j++] = c;
        }
      }
      else{
        if(c != '\r'){
          pass[j++] = c;
        }
      }
    }
    //Closing the string
    pass[j++] = '\0';
    // close the file:
    myFile.close();
  }
  else {
    // if the file didn't open, print an error:
    Serial.println(F("error opening file."));
  }

  Serial.println(ssid);
  Serial.println(pass);

  connectWifi(ssid, pass);
}