2013-01-22 16 views
3

我想打開一個文件,我計算出一個字符串的名稱。但是,它只是給我編譯錯誤,如圖所示。Arduino打開SD文件名作爲字符串

for(int i=1;;i++) 
{ 
    String temp = "data"; 
    temp.concat(i); 
    temp.concat(".csv"); 
    if(!SD.exists(temp))//no matching function for call to sdclass::exists(String&) 
    { 
     datur = SD.open(temp,FILE_WRITE); 
    } 
} 

我是一個java人,所以我不明白爲什麼這是行不通的。我嘗試了幾個字符串對象方法,但似乎沒有工作。我在arduino編程方面有點新,但我更瞭解java。這個for循環的重點是每次arduino重新啓動時創建一個新文件。

回答

9

SD.open需要一個字符數組而不是String,您需要首先使用toCharArray方法對其進行轉換。嘗試

char filename[temp.length()+1]; 
    temp.toCharArray(filename, sizeof(filename)); 
    if(!SD.exists(filename)) { 
    ... 
    } 

完成代碼:

for(int i=1;;i++) 
{ 
    String temp = "data"; 
    temp.concat(i); 
    temp.concat(".csv"); 
    char filename[temp.length()+1]; 
    temp.toCharArray(filename, sizeof(filename)); 
    if(!SD.exists(filename)) 
    { 
     datur = SD.open(filename,FILE_WRITE); 
     break; 
    } 
} 

你會發現許多功能,採取字符數組,而不是字符串。

+0

非常感謝! working – Wijagels

+0

爲什麼這些函數不能接受'const char *',比如'temp.c_str()'返回的函數?這會讓生活變得如此簡單。 SD.exists函數無意改變文件名。 – sauerburger