France
Offline
Newbie
Karma: 0
Posts: 8
|
 |
« on: February 19, 2013, 11:43:11 am » |
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. 
|
|
|
|
« Last Edit: February 20, 2013, 06:23:12 am by Student_42 »
|
Logged
|
|
|
|
|
Offline
Edison Member
Karma: 34
Posts: 1215
|
 |
« Reply #1 on: February 19, 2013, 07:07:13 pm » |
Use 8.3 format. The maximum filename is 8 characters and 3 after the dot.
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 311
Posts: 35470
Seattle, WA USA
|
 |
« Reply #2 on: February 19, 2013, 07:08:19 pm » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
France
Offline
Newbie
Karma: 0
Posts: 8
|
 |
« Reply #3 on: February 20, 2013, 04:58:26 am » |
Hi. Thanks for your advice, i change my code but i doesn't work...  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)
|
|
|
|
« Last Edit: February 20, 2013, 05:15:40 am by Student_42 »
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 311
Posts: 35470
Seattle, WA USA
|
 |
« Reply #4 on: February 20, 2013, 07:21:00 am » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
France
Offline
Newbie
Karma: 0
Posts: 8
|
 |
« Reply #5 on: February 20, 2013, 07:29:52 am » |
My bad for " creation_cph.txt " . My teacher said to use the String... I never use sprintf(), do you have a link for a explanation about it? Thanks a lot !
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 311
Posts: 35470
Seattle, WA USA
|
 |
« Reply #6 on: February 20, 2013, 07:38:52 am » |
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...
|
|
|
|
|
Logged
|
|
|
|
|
France
Offline
Newbie
Karma: 0
Posts: 8
|
 |
« Reply #7 on: February 20, 2013, 07:43:21 am » |
I don't use google but " duckduckgo " .
I will post my new code soon, i need your opinion .
Thanks.
|
|
|
|
|
Logged
|
|
|
|
|
France
Offline
Newbie
Karma: 0
Posts: 8
|
 |
« Reply #8 on: February 20, 2013, 07:50:55 am » |
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++; }
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 311
Posts: 35470
Seattle, WA USA
|
 |
« Reply #9 on: February 20, 2013, 08:14:03 am » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
France
Offline
Newbie
Karma: 0
Posts: 8
|
 |
« Reply #10 on: February 20, 2013, 08:31:11 am » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 311
Posts: 35470
Seattle, WA USA
|
 |
« Reply #11 on: February 20, 2013, 08:44:20 am » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
France
Offline
Newbie
Karma: 0
Posts: 8
|
 |
« Reply #12 on: February 20, 2013, 09:54:13 am » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 311
Posts: 35470
Seattle, WA USA
|
 |
« Reply #13 on: February 20, 2013, 12:37:40 pm » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
France
Offline
Newbie
Karma: 0
Posts: 8
|
 |
« Reply #14 on: February 21, 2013, 03:19:16 am » |
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).
|
|
|
|
|
Logged
|
|
|
|
|
|