Home > Blockchain >  How can i concatenate a const char with char array?
How can i concatenate a const char with char array?

Time:09-29

The array itself is held at

char filestring[9];

and initialized via

snprintf_P(filestring,
           sizeof(filestring),
           PSTR("uuu"),
           dt.Year(),
           dt.Month(),
           dt.Day());

How can i concatenate all above as in the example? (Add the slash and the .txt extension to the filestring variable)

File file = SD.open("/"   filestring   ".txt", FILE_APPEND);

I get the following misleading error for the example above.

expression must have integral or unscoped enum type

CodePudding user response:

Maybe something like this:

char filename[MAX_PATH] = {0};
int n = snprintf(filename, sizeof(filename), "/%s.txt", filestring);
// check whether snprintf succeeded
if (n > 0 && n < sizeof(filename)) {
    File file = SD.open(filename, FILE_APPEND);
}

Update: As requested by a user I am adding a clarification on MAX_PATH:

The line

char filename[MAX_PATH] = {0};

Defines a character array of size MAX_PATH. That could have used any integer value that you thought right for your program but, using MAX_PATH ensures the buffers can hold any filename.

On Linux, you must #include <limits.h> (or you can #include <stdio.h> and use FILENAME_MAX). I am not a Windows user but it looks like you have to #include <stdlib.h> to import MAX_PATH (doc).

Of course you could also also initialized filestring with the desired format in one go:

char filestring[MAX_PATH];
snprintf_P(filestring,
           sizeof(filestring),
           PSTR("/uuu.txt"),
           dt.Year(),
           dt.Month(),
           dt.Day());

CodePudding user response:

In C:

const int size = MAX_PATH;
char path[size];

int rc = snprintf(path, size, "/%s.txt", filestring);
if (rc < 0) {
    fprintf(stderr, "Concatenation error.\n");
} else if (rc > size) {
    fprintf(stderr, "Buffer is too small.\n");
} else {
    printf("path: %s\n", path);
    // Use it...
}

In C (since you tagged your question C ):

std::string path = "/"   std::string(filestring)   ".txt";
File file = SD.open(path.c_str(), FILE_APPEND);

CodePudding user response:

Here's an alternative using a std::ostringstream to build the filename and a std::string to pass the result around to other functions:

#include <iomanip>
#include <sstream>
#include <string>

void some_function() {
           
    std::ostringstream os;

    // build the string using the std::ostringstream
    os << std::setfill('0')
       << '/'
       << std::setw(4) << dt.Year()
       << std::setw(2) << dt.Month()
       << std::setw(2) << dt.Day()
       << ".txt";

    // extract the result into a std::string
    std::string filestring(os.str());
    
    // Then depending on the SD.open() interface:

    // 1. The preferred:
    File file = SD.open(filestring, FILE_APPEND);

    // 2. Backup version:
    File file = SD.open(filestring.c_str(), FILE_APPEND);
}
  •  Tags:  
  • c
  • Related