Reputation: 192
I am working on VS2010 mixed programming and use the Intel Compiler / VisualStudio (Intel Parallel 2015) to compile my project in VS 2010.
console - c source:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
//#define readfile readfile_
extern void readfile(char*, int*);
int main()
{
int n, count;
char Fname[9];
char Name[10];
strcpy(Fname,"AMIN.for");
fprintf( stderr, "%s\n", Fname);
readfile( Fname, &n , strlen (Fname));
fprintf( stderr, "n = %d\n", n);
// fprintf( stderr, "%s\n", Name);
getch();
return 0;
}
subroutine - Lib fortran:
subroutine readfile( fname1, m )
character fname1*(*)
integer m
integer iounit,i
iounit=15
write(*,*) fname1
c10 format (a9)
open(iounit,file = fname1,action='read')
read (iounit,*) m
c20 format (i10)
write(*,*) m
close(iounit)
return
end subroutine
In following line I have a question
readfile( Fname, &n, strlen(Fname) );
I want to change to:
readfile( Fname, &n );
Is it possible?How to call without length of string?
Upvotes: 1
Views: 110
Reputation: 60018
The Fortran routine needs to know the length of the string.
What you are doing right now is that you are using a hidden argument that is used in this particular compiler calling conventions. The argument carries the information about the length of the string.
Fortran does not use null delimited string and therefore you must always know how long is your string.
Now you could change the Fortran procedure using bind(C)
to get rid of the hidden argument, but again, you have to know the length somehow and you would have to convert from character arrays to a string. That would complicate the Fortran part substantially.
In Intel Fortran, which you use, you can use some compiler directives to change the calling conventions http://www.csbi.mit.edu/technology/intel_fce/doc/main_for/mergedProjects/bldaps_for/common/bldaps_hndl_charstr.htm , but be careful, you must know the length of the string in Fortran anyway. Perhaps by declaring it constant.
Upvotes: 1