Call c_str() function from String class for upload file txt to storage

I've tried the File Upload program to upload files stored on the SD Card to Firebase Storage and it worked fine. then due to a datalogger need, I created a dynamic file naming system function based on month-day on RTC. then the format of the file name is created in a string. when I try again it causes an error in the upload process when I create an upload function like this. When FileName variable used as localFileName and remoteFilename in the upload function, which required const char*, need to call c_str() function from String class. FileName.c_str()need help with this, How to call the String FileName that i had created and write it for syntax?

DateTime now = rtc.now();
  FileName[0] = now.day()/10 + '0';
  FileName[1] = now.day()%10 + '0';
  dayupg=now.day();
  FileName[2] = now.month()/10 + '0';
  FileName[3] = now.month()%10 + '0';
  monthupg=now.month();
  FileName[4] = (now.year()/10)%10 + '0';
  FileName[5] = now.year()%10 + '0';
  yearupg=now.year();

  if (Firebase.ready() && currentTime - timeStorage >= intervalStorage)
  {  
    Serial.print("Upload file : ");
    Serial.println(FileName);
    if (Firebase.Storage.upload(&fbdo, STORAGE_BUCKET_ID, String("/" + FileName).c_str(), mem_storage_type_sd, FileName.c_str(), "data.txt"))
      Serial.printf("\nDownload Link: %s\n", fbdo.downloadURL().c_str());
    else
      Serial.println(fbdo.errorReason());
    timeStorage = currentTime;
  }

the error :


** error: invalid operands of types 'const char [2]' and 'char [11]' to binary 'operator+'
278 | if (Firebase.Storage.upload(&fbdo, STORAGE_BUCKET_ID, String("/" + FileName).c_str(), mem_storage_type_sd, FileName.c_str(), "data.txt"))
| ~~~ ^ ~~~~~~~~
| | |
| | char [11]
| const char [2]
error: request for member 'c_str' in 'FileName', which is of non-class type 'char [11]'
278 | if (Firebase.Storage.upload(&fbdo, STORAGE_BUCKET_ID, String("/" + FileName).c_str(), mem_storage_type_sd, FileName.c_str(), "data.txt"))
| ^~~~~
exit status 1
invalid operands of types 'const char [2]' and 'char [11]' to binary 'operator+'
**

Welcome,

There is no need to convert a C-string to a String then back to a C-string. Use functions strcat or sprintf instead. Here is an example : OAfVZy - Online C++ Compiler & Debugging Tool - Ideone.com

1 Like

the + operator only works on Strings not char[ ]s. The problem is FileName is NOT an Arduino String but a char [11]
so do

String fileNameStr = "/";
fileNameStr += FileName;
if (Firebase.Storage.upload(&fbdo, STORAGE_BUCKET_ID, fileNameStr .c_str(), mem_storage_type_sd, FileName, "data.txt"))

P.s. if you want to use strcat don't, use strlcat instead it is much safer for an example of using strlcat see strlcpy_strlcat.ino
Don't use sprintf either see C static code analysis for the snprintf safer alternative and https://joequery.me/code/snprintf-c/

thanks for replying, the link you provided helped me to understand about string class

I applied whatever example you gave, and it works according to what I expected
String fileNameStr = "/"; fileNameStr += FileName; if (Firebase.Storage.upload(&fbdo, STORAGE_BUCKET_ID, fileNameStr .c_str(), mem_storage_type_sd, FileName, "data.txt"))

thanks for helping!

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