简体   繁体   English

如何将 const char 与 char 数组连接起来?

[英]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) (将斜杠和.txt 扩展名添加到文件字符串变量中)

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 :更新:根据用户的要求,我在MAX_PATH上添加了一个说明:

The line线

char filename[MAX_PATH] = {0};

Defines a character array of size MAX_PATH .定义大小为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.这可以使用您认为适合您的程序的任何 integer 值,但是使用MAX_PATH可确保缓冲区可以保存任何文件名。

On Linux, you must #include <limits.h> (or you can #include <stdio.h> and use FILENAME_MAX ).在 Linux 上,您必须#include <limits.h> (或者您可以#include <stdio.h>并使用FILENAME_MAX )。 I am not a Windows user but it looks like you have to #include <stdlib.h> to import MAX_PATH ( doc ).我不是 Windows 用户,但看起来您必须#include <stdlib.h>才能导入MAX_PATH ( doc )。

Of course you could also also initialized filestring with the desired format in one go:当然你也可以在一个filestring中用你想要的格式初始化文件字符串:

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

In C:在 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++ ):在 C++ 中(因为你标记了你的问题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:这是使用std::ostringstream构建文件名和std::string将结果传递给其他函数的替代方法:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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