简体   繁体   English

将const char和int合并为char *

[英]Join const char with int into a char*

Given this variables: 给定以下变量:

const char* Filename="file";
int size=100;
int num=300;

I want to put them together in one char* with this format "file_num_size" (file_300_100 in this case), so what in c++ would be as easy as do: 我想将它们放在一起,以这种格式“ file_num_size”(在这种情况下为file_300_100)放在一个char *中,因此在c ++中将很容易做到:

std::string newFile =Filename+std::to_string(size)+std::to_string(num);

How could I do it in C on the var char* newFile; 我如何在var char* newFile; C语言中完成此操作char* newFile; ?? ??

Thanks 谢谢

--------------EDIT---------------- - - - - - - - 编辑 - - - - - - - -

Thanks to all, I guess not setting a size to the var newFile would be a bit hard to achieve, so i will just go with sprintf and setting a size to newfile. 归功于所有,我想不为var newFile设置大小会有些困难,因此我将只使用sprintf并将大小设置为newfile。 THanks again. 再次感谢。

Roughly like this: 大致像这样:

const char* Filename="file";
int size = 100;
int num = 300;
char newFile[50];    // buffer for 50 chars
sprintf(newfile, "%s_%d_%d", Filename, num, size);

You need to make sure that the filename is never longer than 49 chars. 您需要确保文件名不得超过49个字符。

If the length of filename can be very long this variant may be better: 如果文件名的长度可以很长,则此变体可能更好:

const char* Filename="very_long_file_name_fooo_bar_evenlonger_abcde_blabla";
int size = 100;
int num = 300;
char *newFile = malloc(strlen(Filename) + 30);  // computing needed length
sprintf(newfile, "%s_%d_%d", Filename, num, size);
...
free(newFile);    // free the buffer, once you're done with it, but only then

The + 30 is a quick and dirty way to make room for the _xxx_yyy . + 30是为_xxx_yyy腾出空间的_xxx_yyy There is room for improvement here. 这里还有改进的空间。

If you are using c99 or higher version of C compiler they support variable length arrays, where we can allocate an auto array (on stack) of variable size. 如果您使用的是c99或更高版本的C编译器,则它们支持可变长度数组,我们可以在其中分配可变大小的自动数组(在堆栈上)。

Assuming that size and num are always the positive integers, you can do: 假设sizenum始终是正整数,则可以执行以下操作:

#include <stdio.h>
#include <string.h>

int getSize (int n) {
    int size = 1;
    while (n > 9) {
            n /= 10;
            size++;
    }
    return size;
}

int main()
{
    const char* Filename="file";
    int size = 100;
    int num = 300;

    int total_size = strlen(Filename)+getSize(size)+getSize(num)+1;

    char newFile[total_size]; //Variable length array

    sprintf(newFile, "%s_%d_%d", Filename, num, size);

    printf ("newfile : %s\n", newFile);

    return 0;
}

In compile-time (fastest, but can't be changed): 在编译时(最快,但是不能更改):

#define FILE "file"
#define NUM 300
#define SIZE 100

#define STRINGIFY(x) #x
#define NEWFILE(file, num, size) file "_" STRINGIFY(num) "_" STRINGIFY(size)

...
const char* Filename = FILE;
int num = NUM;
int size = SIZE;
const char* newFile = NEWFILE(FILE, NUM, SIZE); // "file_300_100"

In run-time, slow but readable: 在运行时,速度较慢但可读:

sprintf(str, "%s_%d_%d", file, num, size);

(Where str is large enough to contain the result - this has to be checked in advance) str足够大以包含结果的地方-必须事先检查)

Faster run-time versions do the integer to string conversion manually, then build the string manually step by step. 更快的运行时版本会手动执行整数到字符串的转换,然后逐步手动构建字符串。

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

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