Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

write adding crlf to file?

MWind2
New Contributor III
2,769 Views

I was testing whether I was passing a good char_array to a fortran dll from csharp and wondered why the file written had a crlf in it? Does some specifier prevent such or it default behavior? 

integer function test2(cstr, ilen)
!DEC$ ATTRIBUTES DLLEXPORT, alias : "test2" :: test2
use,INTRINSIC :: ISO_C_BINDING
type(c_ptr), intent(in) :: cstr
integer, intent(in) :: ilen
character, allocatable :: charx(:)
integer :: ilen_max = 512
integer(1), pointer :: fstr(:)
integer :: i 
!

CALL C_F_POINTER(cstr, fstr, [ilen])
allocate(charx(0:ilen))
i=0
do while (i<ilen)
    charx(i)=CHAR(fstr(i+1))
    i=i+1
enddo
 charx(ilen)= CHAR(0)
 write (*,*) charx, ilen,ilen_max
open(1001, file = 'charx.dat', status = 'new')
write (1001,*) charx
close(1001)
test2=ilen
return
end function test2

 

static class Program { 
    static extern unsafe int test2(ref char[] aC, ref int ilength);
        static unsafe void Main(string[] args)

        {
            string sf = "D:\\c\\vs2022\\mmff\\fDll1\\x64\\Debug\\fdll1.dll";
            char[]acf=sf.ToCharArray();
            char[] acff = ToCharacterArrayFortran(sf, sf.Length);
            int iz = sf.Length;
            int iy = test2( ref acf, ref iz);
        }
}

 

0 Kudos
4 Replies
jimdempseyatthecove
Honored Contributor III
2,614 Views
0 Kudos
MWind2
New Contributor III
2,451 Views

Thanks for info. I think now I can get it. 

0 Kudos
MWind2
New Contributor III
2,291 Views

I went with stream to get rid of extras

integer function test2(cstr, ilen)
!DEC$ ATTRIBUTES DLLEXPORT, alias : "test2" :: test2
use,INTRINSIC :: ISO_C_BINDING
type(c_ptr), intent(in) :: cstr
integer, intent(in) :: ilen
character, allocatable :: charx(:)
integer :: ilen_max = 512
integer(1), pointer :: fstr(:)
integer :: i
!
CALL C_F_POINTER(cstr, fstr, [ilen])
allocate(charx(0:ilen))
i=0
do while (i<ilen)
charx(i)=CHAR(fstr(i+1))
i=i+1
enddo
charx(ilen)= CHAR(0)
write (*,*) charx, ilen,ilen_max
open(1001, file = 'charx2.dat', status = 'REPLACE', access='STREAM')
write(1001),charx
close(1001)
test2=ilen
return
end function test2
0 Kudos
cryptogram
New Contributor I
2,562 Views

Welcome to the world of carriage control options.   

 

The first computer I worked on used an electronic typewriter as its printer.  In a typewriter the paper is held in a carriage that moves back and forth in front of the spot where the keys strike. There are two distinct operations involved when starting a new line.  Carriage return moves the carriage back to the first column position.  This can be used by itself to reset the carriage so that you can write over the same line again, perhaps for underlining.  The line feed operation moves the paper up by one line.  Combining a carriage return and line feed sets you up to start a new line.

 

Eventually,  text files became a thing, and for files that don't have a fixed number of columns per line, you need some kind of record separator to indicate where records end.   On the DOS PC side of the world, the record separator was carriage return + line feed.  Unix, on the other end, went for a single byte record separator, and selected the line feed character.

 

These days, the record separator you get by default on windows machine might differ between compiler brands and versions.

However you can definitely get what you really want, either by fooling with the command line options  Fortran...Run -Time....Default Output Carriage Control,  or by specifying the  CARRIAGECONTROL option in the OPEN statement, or by using various options in the

FORMAT statement.

 

 

 

 

 

 

 

 

Reply