我正在尝试打开一个将名称计算为字符串的文件。但是,它只是给了我如图所示的编译错误。
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重启时都会创建一个新文件。
最佳答案
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;
}
}
您会发现许多函数采用char数组而不是字符串。