I want to create a new file on an SD card whenever a button is pressed. To assign new name for the file I have used the following code:
void open_file()
{
byte a=1;
String b= String(a) + ".csv";;
while(SD.exists(String b))
a=a++;
SD.open(String(a)+".csv", FILE_WRITE);
}
This is giving me an error: "expected primary expression before 'b' ".
However, when I replace String b with eg: "xps.csv", it works. I really don't get it "xps.csv is a string and why is it that I cannot use a string variable inside SD.exists??
Please help
The String class is making your code needlessly complex, as well as introducing the possibility of memory corruption in versions prior to 1.0.4 and heap fragmentation in all versions.
Why don't you generate the file name directly in a plain old c-string (i.e. null-terminated char array)? Just declare a char array of the maximum length of your file name and use snprintf() to generate the file name from the number. This will also give you control over leading zeroes etc, which might be useful if you want your files to be sortable alphabetically.
Once it is working you might also want to think about how you will handle or avoid the device filling up.