fortran pass character * 81数组转换为c / c ++代码 [英] fortran pass character*81 array to c/c++ code

查看:205
本文介绍了fortran pass character * 81数组转换为c / c ++代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是编程新手,我想在我的c ++代码中调用fortran函数。事情是我不知道如何传递一个fortran字符* 81数组到我的c ++。

fortran代码是这样的:

 子程序func01(a)
隐式无
字符* 81 a(2)
写(*,*)a(1)
写(*,*)a(2)
结束



c ++ code is例如:

  #include< iostream> 

externC{
void func01_(const char ** a);
}

int main()
{
const char * a [2];
a [0] =Hello world!;
a [1] =这是测试!;
func01_(a);
返回0;
}

我用这个基本测试了我的fortran代码

  program pro01 
character * 81 a(2)
a(1)='Hello world!'
a(2)= '这是一个测试!'
call func01(a)
end program pro01



<



感谢@PaulMcKenzie,我纠正了一些傻瓜问题.....



然而,当我编译cpp代码时,结果就像乱七八糟的代码一样:

  7 
@ L
@ n @ UH j FP
@ n U շ = ྼ @

我该怎么办?

解决方案

以下代码似乎适用于Linux(x86_64)上的gcc4,但尚不清楚它是否也适用于其他平台。 (如上所述,现代Fortran的C-interoperability可能是有用的。)

func01.f90



<$ p $ (a)
字符(*):: a(2)
print *
print *,char length =,len(a 1)),len(a(2))
print *,raw a(1):[,a(1),]
print *,raw a(2) [,a(2),]
print *,trim:[,trim(a(1)),] [,trim(a(2)),]
end

main.cpp

  externC{
void func01_(char * c,const int len);
}

#include< iostream>
#include< cstring> //为memset()
int main()
{
const int lenmax = 30,numstr = 3; //将字符长度改为30以适应终端
char a [numstr] [lenmax];
std :: string str [numstr];

str [0] =moon; str [1] =水银; str [2] =jupiter; (int k = 0; k memset(a [k],'',lenmax);

。 //填充空格
str [k] .copy(a [k],lenmax); //拷贝至多lenmax char(不附加\0)
}

func01_(a [0],lenmax);
func01_(a [1],lenmax); //通过水银
返回0;

$ / code>

编译

  $ g ++ func01.f90 main.cpp -lgfortran 

结果

 字符长度= 30 30 
raw a(1):[moon]
raw a(2) :[汞]
修剪:[月亮] [汞]

字符长度= 30 30
raw a(1):[mercury]
raw a(2 ):[jupiter]
trim:[mercury] [jupiter]


I am a fresh in programming, I wanna to call a fortran function in my c++ code. the thing is I dont know how to pass a fortran character*81 array to my c++.

fortran code is like:

subroutine func01(a)
    implicit none
    character*81 a(2)
    write(*,*) a(1)
    write(*,*) a(2)
end

c++ code is like:

#include <iostream>

extern "C"{
    void func01_( const char **a );
}    

int main()
{
    const char *a[2];
    a[0]="Hello world!";
    a[1]="This is a test!";
    func01_(a);
    return 0;
}

I bascially tested my fortran code using this

program pro01
    character*81 a(2)
    a(1)='Hello world!'
    a(2)='This is a test!'
    call func01(a)
end program pro01

'func01(a)' works well.

thanks to @PaulMcKenzie, I corrected some fool problems.....

However, when i compiled cpp code, the result went like messy codes like:

 7
@L
@��n��@�UH�j��FP
 @��n���U�շ�=��U�ྼ���   @��

what should I do?

解决方案

The following code seems to work for gcc4 on Linux(x86_64), but it is not clear whether it is also valid for other platforms. (As suggested above, C-interoperability of modern Fortran may be useful.)

func01.f90

subroutine func01( a )
    character(*) :: a( 2 )
    print *
    print *, "char length = ", len(a(1)), len(a(2))
    print *, "raw a(1) : [", a(1), "]"
    print *, "raw a(2) : [", a(2), "]"
    print *, "trim     : [", trim(a(1)), "] [", trim(a(2)), "]"
end

main.cpp

extern "C" {
    void func01_( char *c, const int len );
}

#include <iostream>
#include <cstring>  // for memset()
int main()
{
    const int lenmax = 30, numstr = 3; // changed char length to 30 to fit in the terminal
    char a[ numstr ][ lenmax ];
    std::string str[ numstr ];

    str[0] = "moon"; str[1] = "mercury"; str[2] = "jupiter";

    for( int k = 0; k < numstr; k++ ) {
        memset( a[k], ' ', lenmax );  // fill space                                              
        str[k].copy( a[k], lenmax );  // copy at most lenmax char (no \0 attached)                        
    }

    func01_( a[0], lenmax );
    func01_( a[1], lenmax ); // pass from mercury
    return 0;
}

Compile

$ g++ func01.f90 main.cpp -lgfortran

Result

char length =           30          30
raw a(1) : [moon                          ]
raw a(2) : [mercury                       ]
trim     : [moon] [mercury]

char length =           30          30
raw a(1) : [mercury                       ]
raw a(2) : [jupiter                       ]
trim     : [mercury] [jupiter]

这篇关于fortran pass character * 81数组转换为c / c ++代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆