简体   繁体   中英

How can i concatenate a const char with char array?

The array itself is held at

char filestring[9];

and initialized via

snprintf_P(filestring,
           sizeof(filestring),
           PSTR("%04u%02u%02u"),
           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

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("/%04u%02u%02u.txt"),
           dt.Year(),
           dt.Month(),
           dt.Day());

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);

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM