SD library: X.write("X.txt", FILE_WRITE); replace "X.txt" with String

Hello again.
I am trying to write to my SD card.
I can do so with this line

X.write("X.txt", FILE_WRITE);

But it seems I can't replace "X.txt" with a String that holds the value "X.txt".

String stringX="X.txt";
X.write(stringX, FILE_WRITE);

error: CC10:384: error: no matching function for call to 'File::write(String&, int)'

I appreciate all help!
Thank you.

But it seems I can't replace "X.txt" with a String that holds the value "X.txt".

You CAN replace "X.txt" with a string, though. Quit using Strings. You are just pissing away resources needlessly.

char *fileName = "Things.txt";
char *contents = "Put this in the file";
File file = SD.open(fileName, FILE_WRITE);
if(file)
   file.write(contents);

PaulS:
You CAN replace "X.txt" with a string, though. Quit using Strings. You are just pissing away resources needlessly.

char *fileName = "Things.txt";

char *contents = "Put this in the file";
File file = SD.open(fileName, FILE_WRITE);
if(file)
  file.write(contents);

Thank you so much!
This small change removed over 2k bytes from my program!

What exactly is a "char *fileName", and what are Strings used for then?

What exactly is a "char *fileName"

fileName is a variable, of type pointer to char. You could use an array, too:

char fileName[] = "Things.txt";

Pointers and array are often, but not always, interchangeable.

and what are Strings used for then?

Fragmenting memory. On larger Arduinos (memory-wise), they can dynamically grow, and the methods for dealing with the character data are part of the class. On the smaller Arduinos, they are often a disaster.

cool!
I will read more about pointers, they seem useful.

But another problem has appeared!

This works:

tempWord=wordArray[wordSelector];
wordList.write(tempWord, FILE_WRITE);

This does not work:

tempWord=wordArray[wordSelector];
wordList.write(tempWord + ".txt", FILE_WRITE);

This does not work either:

tempWord=wordArray[wordSelector];
    tempword+=".txt";
    wordList.write(tempWord, FILE_WRITE);

This works:

tempWord=wordArray[wordSelector];
wordList.write(tempWord, FILE_WRITE);

How. The write() method does not take a second parameter with a value of FILE_WRITE.

This does not work:

Of course not. What does your calculator do when you try to ADD tempWord and ".txt"?

This does not work either:

Try this:

char buffer[12];
sprintf(, "%s.txt", tempword);
wordList.write(buffer);

Sorry! My bad. I wrote something wrong.
It still does not work, though.

This does not work:

tempWord=wordArray[wordSelector];
    char buffer[12];
    sprintf("%s.txt", tempWord);
    wordList.write(buffer);
    wordList = SD.open(tempWord, FILE_WRITE);

This does not work:

tempWord=wordArray[wordSelector];
    sprintf("%s.txt", tempWord);
    wordList = SD.open(tempWord, FILE_WRITE);

Also, wordArray is a char *wordArray[50];

So the problem is to add one String to another String Array, but use char* instead of String.

edit: no luck with this either:

tempWord=wordArray[wordSelector];
    char buffer[20];
    sprintf(buffer,"%s.txt", tempWord);
    wordList = SD.open(buffer, FILE_WRITE);

This does not work

sprintf() takes three arguments - where, how, what. You keep leaving out the where. I don't. Why can't you simply copy/paste?

edit: no luck with this either:

http://snippets-r-us.com can help you with your snippets. I can't, without seeing all the variable's types and sizes AND the error messages.

My mistakes. I appreciate what you have done very much, and will give you as much code as needed.
Here is the function and the relevant variables:

void FinishSaveWord()
{
  if(!isItaList)//this works, but is supposed to be replace by the following if() statement
  { 
    wordList = SD.open("wordlist.txt", FILE_WRITE);
  }

  
/*
  if(!isItaList) //I don't know what I'm doing at this point:
  {
    tempWord=wordArray[wordSelector]; 
    //wordSelector is a integer no smaller or bigger than wordArray[] (0-49)

    char buffer[30];
    sprintf(, "%s.txt", tempword);
    //wordList.write(buffer);
    wordList = SD.open(buffer, FILE_WRITE);
  }
*/


//everything below this line works
  if(wordList)
  {
    wordList.write(typedWord);
    wordList.close();
  }
  else
  {
    lcd.setCursor(0,3); 
    lcd.print("Error opening file");
  }
}
File wordList;
char *wordArray[50]; //storage for 50 words that is to become file names
char *tempWord;         //storage for 1 selected word, that will have ".txt" added to it, to make it a filename.

error:

CC10.ino: In function 'void FinishSaveWord()':
CC10:392: error: expected primary-expression before ',' token
expected primary-expression before ',' token