Name of files

Hi !
I have some troubles with my code. =(
I want to change the name of the files in the aim to select them; but the strings are not allow...

My code:

  unsigned long taille_cph = 0 ,taille_cph_MAX = myFile.size();
   int Chaine_Tampon, cpt = 0 ;  // chaine de stockage des données
   String name_a = "Sauvegarde_ph";
   String name_b = ".txt";
   String name ;
   
    while(taille_cph <= taille_cph_MAX){
      myFile = SD.open("creation_cph.txt", FILE_READ); // open "creation_cph.txt"
      Chaine_Tampon = myFile.read(); 
      myFile.close(); // close "creation_cph.txt"  
    
      myFile = SD.open( (noma + cpt ) +nomb,  FILE_WRITE);
      myFile.println(Chaine_Tampon,DEC);
      myFile.close();
      cpt++;
    }

Thanks for your help. :slight_smile:

Use 8.3 format. The maximum filename is 8 characters and 3 after the dot.

Fat16, used by SD cards, supports 8.3 names, only.

The open() method knows that Strings are crutches. and has deemed them not worth supporting. I won't argue with that position.

Hi. Thanks for your advice, i change my code but i doesn't work... :frowning:
My aim is to "clone" the file call "creation_cph.txt" in a new file than i could select.

   unsigned long taille_cph = 0 ,taille_cph_MAX = myFile.size();
   int Chaine_Tampon, cpt = 0 ;  // String of storage
   String name_a = "Svg_ph";
   String name_b = ".txt";
   String name = 0;
   
    while(taille_cph <= taille_cph_MAX){
      myFile = SD.open("creation_cph.txt", FILE_READ); // open "creation_cph.txt"
      Chaine_Tampon = myFile.read(); 
      myFile.close(); // close "creation_cph.txt"  
 
      name = (name_a + cpt ) + name_b ;
    
      myFile = SD.open( name , FILE_WRITE); // ERROR
      myFile.println(Chaine_Tampon,DEC);
      myFile.close();
      cpt++;
    }

The compilator said:

Programme_Metronome_2.cpp: In function 'void fonction_cph()':
Programme_Metronome_2:253: error: no matching function for call to 'SDClass::open(String&, int)'
C:\Users\DUDE\Desktop\Projet_LP\arduino-1.0.1\libraries\SD/SD.h:74: note: candidates are: File SDClass::open(const char*, uint8_t)

The compilator said:

Not to use Strings. So, don't. There is no reason for using Strings.

sprintf() and a char array are sufficient.

creation_cph.txt doesn't look like 8.3 format.

My bad for " creation_cph.txt " . My teacher said to use the String... :0

I never use sprintf(), do you have a link for a explanation about it?
Thanks a lot !

I never use sprintf(), do you have a link for a explanation about it?

Hard to believe that google failed you.

I'll hold up my book, though. Here...

I don't use google but " duckduckgo " .

I will post my new code soon, i need your opinion .

Thanks.

I'm back !
It's more a copy/paste of tutorial see on the web than my own code for the sprintf().
But the complilator looks happy. What do you thinks about it ?

  unsigned long taille_cph = 0 ,taille_cph_MAX = myFile.size();
   int Chaine_Tampon, cpt = 0 ;  // String of storage
   char name_a[] = "Svg_ph";
   char name_b[] = ".txt";
   char name[256];
   
    while(taille_cph <= taille_cph_MAX){
      myFile = SD.open("crea_cph.txt", FILE_READ); // open "creation_cph.txt"
      Chaine_Tampon = myFile.read(); 
      myFile.close(); // close "creation_cph.txt"  
   
      sprintf(name, "%s%s%d", name_a, name_b, cpt);  
    
      myFile = SD.open( name , FILE_WRITE);
      myFile.println(Chaine_Tampon,DEC);
      myFile.close();
      cpt++;
    }
   char name_a[] = "Svg_ph";
   char name_b[] = ".txt";
   char name[256];
   
      sprintf(name, "%s%s%d", name_a, name_b, cpt);

This is going to create files named Svg_ph.txt0, Svg_ph.txt1, Svg_ph.txt2, Svg_ph.txt3, Svg_ph.txt4, etc.

Probably not what you want.

You can simplify that code:
   char name[256];
   
      sprintf(name, "Svg_ph%d.txt", cpt);

(Or wherever you actually want the numeric portion to be.)

  unsigned long taille_cph_MAX = myFile.size();

Where was myFile initialized? Why is the size of that file in bytes relevant?

    while(taille_cph <= taille_cph_MAX)
    {
    }

When is this loop ever going to end? taille_cph never changes, so 0 will always be less than another value, as long as that other value is greater than 0.

Hi again.
New code :

unsigned long taille_cph = 0 ,taille_cph_MAX = myFile.size();
   unsigned long Chaine_stockage;  // Chaine de stockage des informations
   int cpt = 0;
   char nom[256];
   
    while(taille_cph <= taille_cph_MAX){
      myFile = SD.open("crea_cph.txt", FILE_READ); // ouvrir "crea_cph.txt"
      Chaine_stockage = myFile.read(); 
      myFile.close(); // fermer "crea_cph.txt"
      
      sprintf(nom, "Svg_ph%d.txt", cpt);
    
      myFile = SD.open( nom , FILE_WRITE);
      myFile.println(Chaine_stockage,DEC);
      myFile.close();
      cpt++;
      taille_cph++;
    }

For the initialisation of my file it is before this part of my code.
And it's that for the sd card :

// SD Card
File myFile;
/******* INITIALISATION *******/   
   
    Serial.begin(9600); // Init la communication en SPI
    while (!Serial) { ;} // attend la connection d'un port
    digitalWrite(7,HIGH);// si la communication en SPI est présente segment F ON
    
    if (!SD.begin(10)){ // Test si absence carte SD
     digitalWrite(A5, HIGH); // si oui Led rouge ON  
     return;}
    else{ digitalWrite(2,HIGH); }// si la SD_Card est présente segment A ON 
      
    if (SD.exists("crea_cph.txt")) {} // vérifie si le fichier éxiste
    else { 
      myFile = SD.open("crea_cph.txt", FILE_WRITE); // sinon création de celui ci
      myFile.close();
      } 
    if (SD.exists("crea_cph.txt")) {}   // vérifie si le fichier éxiste
    else { digitalWrite(A5, HIGH);} // si échec de l'initialisation Led rouge ON     
    digitalWrite(3,HIGH); // si réussite de l'initialisation SD_Card segment B ON

Sorry but i don't understand your question " Why is the size of that file in bytes relevant? " .

Thanks for your help.

Tools + Auto Format would be a good idea, before you post ALL of your code.

Why have cpt and taille_cph? They appear to serve the same purpose.

Sorry but i don't understand your question " Why is the size of that file in bytes relevant? " .

The number of times that this while loop:

    while(taille_cph <= taille_cph_MAX){
      myFile = SD.open("crea_cph.txt", FILE_READ); // ouvrir "crea_cph.txt"
      Chaine_stockage = myFile.read(); 
      myFile.close(); // fermer "crea_cph.txt"
      
      sprintf(nom, "Svg_ph%d.txt", cpt);
    
      myFile = SD.open( nom , FILE_WRITE);
      myFile.println(Chaine_stockage,DEC);
      myFile.close();
      cpt++;
      taille_cph++;
    }

iterates is based on taille_cph_MAX, which is initialized:

taille_cph_MAX = myFile.size();

Therefore, the number of times that the loop iterates is based on the size of some file. A tiny file means a few iterations. A huge file means lots of iterations.

I don't see how the size of some mysterious file is relevant.

First steep in this forum, my bad.

I need to explain more my code.
The user will created a file and if it's good for him, he can save it.

The data are saved, in a first time, in "crea_cph.txt".
If he want to save them the data are " clone " in a new file.
This new file is select among the virgin files in the SD card.
In the " Svg_ph%d.txt " the %d (cpt) will be use for select the aviable files.

I hope it will help you for understand.

I hope it will help you for understand.

Posting all of your code would be even better.

The value being stored in taille_cph_MAX is the size of the file, and the while loop is supposed to be opening the original file, reading a byte, and writing that byte to another file that number of times.

Instead, the while loop is opening the file, reading a byte, and writing it to a new file, that number of times. If the original file is 100 bytes, the while loop will create 100 files with one byte each. I'm pretty sure that this is not what you want to do.

You need a while loop to find a file that does not exist. Then, inside that loop, when you find one, you need another loop to copy the data from the old file to the new file.

Hi. My code is totaly under contruction.
I will post all of it soon, it's a school projet in partnership with Snootlab (see the french section for more info).