Need some help with creating files in the SD card

Okay so basically i have this code shown below, it is used to check if a certain file is already in the SD card and if it isn't create it. I am doing this on the Ethernet Shield.

#include <SPI.h>
#include <SD.h>

File myFile;
int filenumber=0,filename=0;
String stringOne, stringTwo, stringThree,stringFour;
boolean existence=true;
void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  
  
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  Serial.print("Initializing SD card...");

  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done."); 
}

void loop() {
  do
  {
  stringOne = String("datalog");
  stringTwo = filenumber;
  stringFour = String(".txt");
  stringThree = stringOne + stringTwo + stringFour;
  
  
  if (SD.exists(stringThree)) 
  {
    Serial.print(stringThree);
    Serial.println(" exists.");
    filename = 0;
    existence = true;
    filenumber++;
  } 
  else 
  {
    Serial.print(stringThree);
    Serial.println(" doesn't exist.");
    filename = 1;
    existence = false;
  }
  delay(1000);
  }while(existence);

  if(filename)
  {
    Serial.print("Creating ");
    Serial.println(stringThree);
    myFile = SD.open(stringThree, FILE_WRITE);
    myFile.close();
    filename = 0;
    existence = true; 
  }
  else
  {
    Serial.println("There is no need to make new file");
  }
  
}

It works "fine" in a way but the problem is once it reaches file 10, it just stays stuck there. Just want to know is that the limit, or is there something wrong with my code?

Well you should not be using the String class as it will possibly come bite you later but the pb you face is file name length. The FAT format supported imposes the use the 8.3 format,

so that file names look like “NAME001.EXT”, where “NAME001” is an 8 character or fewer string, and “EXT” is up to 3 character extension. People commonly use the extensions .TXT and .LOG. It is possible to have a shorter file name (for example, mydata.txt, or time.log), but you cannot use longer file names

(from the documentation)

As you prefix your files with datalog, that's already 7 characters, so datalog0.txt to datalog9.txt works fine as a name but datalog10.txt becomes too long for the 8 characters rule.

It all looks like a pointless exercise in re-inventing the wheel. Just say what filename you intend to use, use it, and close it when you finish.
If the file exists, it will add to it.
If the file does not exist, it will create it.

@J-M-L Oh... I see... Im doing this with the bare minimum knowledge. Anyways thanks for the info. In that case ill just use a shorter name then. About using string, well that is so far the only thing that worked for me that allows for the constant change of the file number, so i thought ill just stick with it.

@Nick_Pyner Yes very aware of that. But what you mention doesn't fit with what i need. What i need is to be able to change the file name everyday/session and log data to it without going to my computer to rewrite the small portion of code so that the file name gets changed.

No you're not, and it's still a pointless exercise. Use the date as a filename and change it every midnight. Then read reply #2 again, twice. This is pretty normal practice.

Agree with Nick there - unless you need special info to be written to the file the first time you create it like a header or something, you can skip the whole thing.

The YYYYMMDD.log name is pretty good to do daily logs splits (YYYY being the year, MM the month number and DD the day with a leading 0 if needed such as 01 02 ... 09 10 11... 31)

It meets the 8 digit rule, is easy to interpret by a human if you are looking out for a special date, is easily organized in increasing or decreasing order (log2.txt would be listed after log188.txt for example in your case)

Do you use a RTC?

@Nick_Pyner Yes i would love to use the date as the file name. I went with this approach as I cant seem to find a way for the Arduino to print out a proper date for me. If you could show me how to do it I would use it.

@J-M-L No. Also what is a RTC? Im gonna assume it is a clock chip?

A Real Time Clock (RTC) is a TimeKeeping Chip giving you the real date such as a DS1302, DS1307 (not too good) or strongly advised to use a DS3231 or DS3232

See arduino time library for the concept - and a frequently used library is the one from Jack Christensen

Really will make it easy to drop the use of Strings and move to char arrays :slight_smile:

Oh.... Okay... Ill see if i can get one easily. For now ill have to go with this approach Im doing.

so how do you know it's time to start a new file?

If you have power loss or a crash (due to the repeated use of Strings for example) how do you get going again?

I would suggest you study sprintf() or itoa() to build a char array and have file names like 00000000.log, 00000001.log, 00000002.log Or log00000.txt, log00001.txt,....

Also Remember for you file counter that a signed int does not go very far (well if it's one file a day that's still a long time but there is a limit)

This is actually just a small part of my main program. I just want to test out how far can it go. Anyways, I have a counter inside my program to state if its time to make another file to log data to.

About the power loss/crash thing, well i guess a hard reset? I haven't really thought that far I was only really concern with getting this part up and running first. And thanks for the suggestion, ill go take a look.

Which Arduino are you using? Uno?

Yes a power cycle is like a new start so you need a way to find which file was the latest file you were writing to as your counter will be reset. (You have built in EEPROM (mind the 100,000 cycle) and a SD card so it can be used to store some general information as well like archiving your counter or something but Murphy says that the crash will happen then :))

I haven't really thought that far I was only really

Very evident, and

Ill see if i can get one easily. For now ill have to go with this approach Im doing.

sounds like a really bad idea.

If you are are asking what an RTC is, you really haven't the faintest idea of what you are about, and I submit you should focus on getting one, come hell or high water, now , at once, immediately, and to the exclusion of anything else in matters Arduino. I'm not being flippant here and indeed, once you do, the futility of the absurd path you propose will become painfully evident. It will also become particularly hurtful when you realise that said path can be avoided for about $2-50 for a DS3231, so I submit you should avoid it right from the start. A DS1307 will be even cheaper, and may suffice, but it is not so accurate in changeable climate.

An example is at
http://homepages.ihug.com.au/~npyner/Arduino/DY_4_Temp_minmax_FLOW_PWR.ino
Essentially the data is recorded on SD every ten seconds, with a new file started at midnight. It is also broadcast via bluetooth @ 1Hz. The files may be downloaded via bluetooth by sending MMDD for current year. Hopefully this will be understandable. I make no great claim for elegant programming and I think there is more than one way of doing this.

You have an ethernet shield. If your ethernet shield can connect to the internet, I suggest using NTP to get the time.

Nick_Pyner:
Very evident, andsounds like a really bad idea.

If you are are asking what an RTC is, you really haven't the faintest idea of what you are about, and I submit you should focus on getting one, come hell or high water, now , at once, immediately, and to the exclusion of anything else in matters Arduino. I'm not being flippant here and indeed, once you do, the futility of the absurd path you propose will become painfully evident. It will also become particularly hurtful when you realise that said path can be avoided for about $2-50 for a DS3231, so I submit you should avoid it right from the start. A DS1307 will be even cheaper, and may suffice, but it is not so accurate in changeable climate.

Nick - everyone needs to start somewhere and getting more Hardware might not be always possible for everyone. We don't know the context - for exploring features about files, saving data on SD, in a timely fashion there is already lots to learn there. We don't know if the OP has plans for something that will run a long time or just toying with ideas for learning purpose.

I think the former and with a timespan over days but, more particularly, he is insisting on the latter. This is why I pointed out the price of an RTC. Even if OP cannot afford one, he certainly needs to know about it, and doing so now makes a hell of alot more sense than running off on a dumb tangent. That is a last resort you only take on after you know about RTCs, and their prices. He says he would love to use date as filename, so I have shown an example.

@SuferTim - Im using a Arduino Mega 2560. Ill go try that out, did see that before never did attempt to use it.

@Nick_Pyner - Okay. Thanks for example ill go have a look.

@J_M_L - Ill try that out as well. Thanks for the tips.

Perhaps I should explain why i mentioned if i can get the RTC easily. Its simply because I want to avoid using my own money because im doing this as part of my school major project, and im doing this out of my own curiosity.

Prinny:
Perhaps I should explain why i mentioned if i can get the RTC easily. Its simply because I want to avoid using my own money

Fair enough. Try to get the school to come good with the money. Getting the time off the internet is indeed a proposition although I found it troublesome for reasons I can't remember and prefer an RTC, which will keep plodding on even if the internet is down. If this really is just a short term demo, NTP should suffice to prove the point. Just be prepared to field the embarrassing question.

Whichever way you go, date as filename should serve your needs. It is bulletproof and common practice.

Can you spare a buck?