简体   繁体   English

如何将常量字符串从fortran函数传递给C

[英]How to pass a constant string from fortran function to C

I'm having troubles to pass a string from Fortran to C. The C code passes a string that contains a filepath to a Fortran function. 我在将字符串从Fortran传递到C时遇到麻烦。C代码将包含文件路径的字符串传递到Fortran函数。 The function reads in the file and should return a two-characters string. 该函数读取文件,并应返回两个字符的字符串。

Here is my C code 这是我的C代码

#include <stdlib.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <sys/time.h>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <time.h>
#include <ctype.h>
using namespace std;

extern "C" void read_id_type_(char *file_path, char *id_type, size_t *path_len_file);

int main(int argc, char **argv) {

char path[500],id_type[2];
char *dir_path, *file_path;
dir_path=path;
size_t path_len=strlen(dir_path);

file_path=strcat(path,"/rnspar_mpt1.dat");
size_t path_len_file=strlen(file_path);

read_id_type_(file_path,id_type,&path_len_file);
 printf("id_type is %s\n",id_type);

 return EXIT_SUCCESS ;
 }

Here is my Fortran function 这是我的Fortran函数

 integer function read_id_type(filename,id_type,path_len) 
 implicit none

 integer :: nrg, nrf, nrf_deform, nrgin,path_len
 character(400) :: filename
 character(2) ::  id_type,EQ_point

 filename=filename(1:path_len)
 write(*,*),"Reading file", filename

 open(unit = 1,file = trim(filename), status='old')
 read(1,'(4i5)') nrg
 read(1,'(4i5)') nrf
 read(1,'(2i5,2(3x,a2))') nrf_deform, nrgin, id_type, EQ_point
 close(1)
 print *,"Id_type=",id_type
 read_id_type = 0
 end function read_id_type

The output is: 输出为:

Id_type=IR    (From the Fortran side)

id_type is IRV2?      (From the C side)

The output, in the C code, should be only the first two characters. 在C代码中,输出应仅是前两个字符。 Any help would be much appreciated 任何帮助将非常感激

Instead of 代替

id_type[2]

make that at least 使至少

id_type[3]

and set the 3 rd character (at index 2) to integer 0 after the call to the Fortran routine, otherwise it won't be a zero-terminated string that C understands. 并设置第三字符(索引2)该呼叫到Fortran例程之后为整数0,否则它不会是零终止的字符串是c理解。


Since the current code tries to output a non-zero-terminated string it accesses memory beyond the array and has formally Undefined Behavior. 由于当前代码试图输出一个非零终止的字符串,因此它访问数组之外​​的内存,并且具有正式的未定义行为。

With UB anything or nothing or possibly what you expect can happen. 使用UB,任何事情或什么都不发生,或者可能发生您期望的事情。

You just got some weird extra characters. 您只是得到了一些奇怪的额外字符。


The string displays correctly in Fortran because you've specified its length there, 该字符串在Fortran中正确显示,因为您已在其中指定了长度,

character(2) ::  id_type

There's not really a corresponding feature in C, but in C++ you could use a std::string of length 2 and pass its buffer to the Fortran routine, C语言中确实没有相应的功能,但是在C ++中,您可以使用长度为2的std::string并将其缓冲区传递给Fortran例程,

std::string id_type( 2, ' ' );

//...
read_id_type_( file_path, &id_type[0], &path_len_file );

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

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