FortranからCへの文字列の受け渡しに問題があります。Cコードは、ファイルパスを含む文字列をFortran関数に渡します。この関数はファイルを読み込み、2文字の文字列を返す必要があります。ここで Fortran関数からCへの定数文字列の受け渡し方法
は私のFortranの機能ここでは私の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 ;
}
である
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
出力は次のようになります。
Id_type=IR (From the Fortran side)
id_type is IRV2? (From the C side)
出力は、Cコードで、唯一でなければなりません最初の2文字。 すべてのヘルプは非常に
はたぶんhttp://stackoverflow.com/q/19913383/577108とhttp://stackoverflow.com/q/9972743/577108はあなたのアイデアを与えることができます。 – haraldkl
ところで、 ''、 ''、 ''、 ''はC++ヘッダーであり、 'using namespace std;'はC++ステートメントです。したがって、このコードはCとしてコンパイルされません。メインコードは純粋なCスタイルですが、C++としてのみコンパイルされます。 –
CからFortranにhttp://stackoverflow.com/questions/8207997/calling-a-fortran-subroutine-from-cの文字列を渡す例があります。私はCとFortranの文字列の違いを理解することをお勧めします。次のステップ:戻り値の引数を追加するためのISO Cバインディングの詳細な調査。スタックオーバーフローには多くの例があります。 –