Creating filename using string or char

I am making a simple datalogger with a SD card.
The idea is that every time the Arduino starts it will make a new logfile fx. sernsor1.txt, sensor2.txt and so on.
Therefore I have a file containing the last number used, but the problem is that something is wrong in the way I make the filename.
In the sketch below i use a String concatenating including the nummber, but the file.open fails because the format of the filename is wrong.

Any hints ?

#include <SdFat.h>
#include <SdFatUtil.h> 
#include <ctype.h>

//Create the variables to be used by SdFat Library
Sd2Card card;
SdVolume volume;
SdFile root;
SdFile file;

String stringOne = "Sensor";
const uint8_t SdChipSelect =5;

char name[] = "Test.txt";     //Create an array that contains the name of our file.
char numname[] = "number.txt";  //name of file containing last number
char contents[256];           //This will be a data buffer for writing contents to the file.
char in_char=0;

  void setup(void)
  {  
      Serial.begin(9600);        //Start a serial connection.
      pinMode(5, OUTPUT);       //Pin 10 must be set as an output for the SD communication to work.
      card.init();               //Initialize the SD card and configure the I/O pins.
      volume.init(card);         //Initialize a volume on the SD card.
      root.openRoot(volume);     //Open the root directory in the volume. 
    
      file.open(root, numname,O_READ); //Read the number
      char num=file.read();
      Serial.println(num);
      file.close();
  
    num++;
  
    file.open(root, numname,O_WRITE); //save new number
    file.print(num);
    file.close();
  
  Serial.println("Number:");
  Serial.println(num);
  //Create new filename and file
  stringOne=stringOne+num+".txt";
  Serial.println(stringOne);
 int strlen=stringOne.length()+1;
 char charBuf[strlen];
  stringOne.toCharArray(charBuf, strlen);
  Serial.println(stringOne);
  //Open and write to new file
 file.open(root,name , O_CREAT | O_APPEND | O_WRITE); // Tested OK
// file.open(root,stringOne , O_CREAT | O_APPEND | O_WRITE); //Problems

file.print("testing");
file.close(); 

 
  }
void loop(void){ 

 file.open(root, numname, O_READ);    //Read the number from numfile
    in_char=file.read();              //Get the first byte in the file.
       while(in_char >=0){            //If the value of the character is less than 0 we've reached the end of the file.
        Serial.print(in_char);    //Print the current character
        in_char=file.read();      //Get the next character
    }
    file.close();    //Close the file
    Serial.println("Delay");
    delay(1000);     //Wait 1 second before repeating the process.

 
  
}

My recommendation is to dump the String class. Use char arrays (static, big enough) and sprintf with proper format specifiers to define the name.

I find myself agreeing with Paul these days a lot (not that I disagree with you at all previously). The OP is suggested to either use plain char array or dig into SdFat.h to see if there exists a method that takes String object as file name.

 file.open(root,name , O_CREAT | O_APPEND | O_WRITE); // Tested OK
// file.open(root,stringOne , O_CREAT | O_APPEND | O_WRITE); //Problems

Note that name is char array, which succeed (what's your definition of tested ok?), while stringOne is String, which failed. You didn't say how it failed though, a common mistake when asking questions. So I am going to be nice and ask:
Did it fail

  1. to compile?
  2. to produce desirable result at run time but compiled fine?

Thanks PaulS and Liudr

You are right, I should have said that the "Problem" solution didn't compile, and now I see why.

I have made newfile a char array, and used sprintf to create the new file name, and now it works fine, that is it compiles and produce the expected results.

sprintf(newfile, "sensor%d.txt", num);

You got ahead of me. I was about to suggest sprintf in case you ask "How am I supposed to do this without a String class?!"

Matching data types may not be critical in many loose-syntax languages but is absolutely critical in C/C++. You open a C book it should start with some intro and then data types and then the language features like loops etc. If the compiler doesn't pick up some of these problems, you will likely shoot yourself in the foot. Some other languages do a lot of processing with data types to make everything work but not in C or even C++.

BTW, I suggest "%02d" in place of "%d", you get better sorted file names (01.txt, ... 10.txt, not 1.txt ... 10.txt) on a computer and maybe later rid the "last file name file" and just do something like folder.lastfilename() to determine the next file name :wink:

BTW, I suggest "%02d" in place of "%d",

Thanks for the tip liudr, that was useful