Use char-array as filename string for sd-module

Hello everyone,

I'm currently integrating a SD-Card-module into my project and I am struggeling to automatically create new filenames correctly.
I have a series of char data i would like to combine to a filename.

This is what I am trying:

char sFilename[]="";
sFilename[0] = 'a';
sFilename[1] = 'b';
sFilename[2] = 'c';
sFilename[3] = 'd';
sFilename[4] = 'e';
sFilename[5] = 'f';
sFilename[6] = 'g';
sFilename[7] = '.';
sFilename[9] = 't';
sFilename[10] = 'x';
sFilename[11] = 't';
sFilename[12] = '\0';    

myFile = SD.open(sFilename, FILE_WRITE);
if (myFile) {
    Serial.print("Writing to file");
    [...]
    return true;
  } 
  else {
    Serial.println("error opening file");
    return false;
  }

The above code does not work, it breaks into the else loop: "error opening file".
It does not matter wether I use the NULL-Marker at the end or not.
It works however if I hard-code the filename entirely like so:
myFile = SD.open("abcdefg.txt", FILE_WRITE);
I feel like i lack some detail about usage of char arrays but most articles in the web do not really help me.
Do you have any idea how to create such a filename properly?
Thanks and best regards!

char sFilename[]="";

This declares an array with zero elements, so by definition elements 0 to 12 do not exist, at least not in the memory allocated to the array

Declare the array with the required number of elements in the first place

char sFilename[]="";
sFilename[0] = 'a';
sFilename[1] = 'b';
sFilename[2] = 'c';
sFilename[3] = 'd';
sFilename[4] = 'e';
sFilename[5] = 'f';
sFilename[6] = 'g';
sFilename[7] = '.';
sFilename[9] = 't';
sFilename[10] = 'x';
sFilename[11] = 't';
sFilename[12] = '\0';

NO no nonononnno...

char sFilename[]="abcdefg.txt";

-jim lee

Thanks for the quick answers.
Problem is that I cannot hardcode it like that:

char sFilename[]="abcdefg.txt";

I recieve the single letters for the name as single chars as a code for the current time.

All I want to do is somehow combine them to look like a "normal" String (or string or whatever the compiler automaticcally thinks a value like "abcdefg.txt" is)
Best regards

I recieve the single letters for the name as single chars as a code for the current time.

Then there are several different ways to turn them into a filename, but however you do it then it is essential to declare the array large enough to hold the resultant string

char sFilename[20]; // Give it some room

int strIndex = 0; // Setup an index..

Every time you get a new char..

sFilename[strIndex] = newChar; // Stuff in the new char.
strIndex++; // bump up the index.

When you are done with this filename..
strIndex = 0; // point at the first char again to fill in a new name.

-jim lee

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.