简体   繁体   English

将char转换为std :: string,然后进行串联

[英]Converting char to std::string, then concatenating

Although I have seen many similar questions, I couldn't find a solution to this particular problematic. 尽管我已经看到许多类似的问题,但是我找不到解决这个特殊问题的方法。

My problem is very simple, yet I'm totally stuck. 我的问题很简单,但我完全陷入困境。 I have a simple c++ function, for which I tried something like 我有一个简单的c ++函数,为此我尝试了类似的方法

void function1(char* filapp) {
   std::string filapp_2 = "";
   fprintf(stdout, "filapp_2: %s\n", filapp_2.c_str());
   filapp_2  += "A";
   fprintf(stdout, "filapp_2: %s\n", filapp_2.c_str());
   filapp_2  += "B";
   fprintf(stdout, "filapp_2: %s\n", filapp_2.c_str());
   filapp_2  += std::string(filapp);
   fprintf(stdout, "filapp_2: %s\n", filapp_2.c_str());
   filapp_2  += "D";
   fprintf(stdout, "filapp_2: %s\n", filapp_2.c_str());
}

If the input value is "C", then this code prints 如果输入值为“ C”,则将打印此代码

filapp_2:
filapp_2: A
filapp_2: AB
filapp_2: ABC
filapp_2: ABC

I tried many other methods, still this is basically always the result. 我尝试了许多其他方法,但这基本上始终是结果。 I tried prepending and it erases "AB". 我尝试过前置,并且会删除“ AB”。 I don't know why that is. 我不知道为什么。

It seems like when filapp is concatenate, the string is no longer dynamic and becomes constant. 似乎当filapp连接时,字符串不再是动态的,而是变为常量。 Maybe it related to the fact that this function is called through a fortran interface, which constrains the memory? 可能与以下事实有关:通过fortran接口调用此函数会限制内存? The interface looks like 界面看起来像

module module_name
    implicit none
    interface
        subroutine function1 (filapp) bind(c)
            use iso_c_binding
            character(kind=c_char) :: filapp(*)
        end subroutine function1
    end interface
end module

EDIT: I was asked to add the fortran part which calls the function. 编辑:我被要求添加调用函数的fortran部分。 So I'm working on a huge, so everything is really intricate. 所以我正在做一个巨大的事情,所以一切都非常复杂。 Basically, the fortran function looks like 基本上,fortran函数看起来像

call function1(filapp)

where filapp is defined as filapp被定义为

character(len=fnlen) :: filapp

which is different then in the interface it-self. 这与界面本身不同。 I'm not sure what this implies. 我不确定这意味着什么。

The std::string constructor std::string(filapp) requires filapp to be null-terminated. std::string构造函数std::string(filapp)要求filapp以null终止。 The actual argument in the Fortran reference to the subroutine will not be null-terminated unless you make an effort for it be be such. 除非您努力做到这一点,否则Fortran中对该子例程的引用中的实际参数不会为空终止。

Consider the example 考虑例子

use, intrinsic :: iso_c_binding, only : c_char, c_null_char
use module_name

call function1(c_char_'C'//c_null_char)
end

and look at what happens without the appended c_null_char . 看看没有附加c_null_char会发生什么。

More generally, a variable declared like 更一般地,声明为

character(len=fnlen, kind=c_char) :: filapp

as a scalar character of length fnlen may be passed as an argument to the assumed-size dummy argument, but you'll again need to have it null-terminated: 因为长度为fnlen的标量字符可能会作为参数传递给假定大小的伪参数,但是您再次需要使它以null终止:

call function1(TRIM(filapp)//c_null_char)

The trim is to avoid having trailing blanks before the termination. 修剪是为了避免在终止之前有尾随的空白。

Alternatively, have the null character in filapp by some means. 或者,通过某种方式在filapp使用空字符。

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

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