I know this is probably not a very smart question to ask, but I have a code written in C that generates a binary file and I have to read the data from this file with Fortran. I'm not really familiar with C but I tried to come up with a solution.
Here is how the file in written in the C program
unsigned nvarnew = 16;
std::vector<float> new_data( ncells*nvarnew, 0.0f );
sprintf(offname,"%s_pot.dat",snapname.c_str());
for( size_t i=0; i<ncells*nvarnew; ++i )
new_data[i] = bytereorder<float>(new_data[i]);
std::ofstream offs(offname,std::ios::binary);
for( size_t i=0; i<ncells; ++i )
{
offs.write( reinterpret_cast<char*>(&blocksize), sizeof(int) );
offs.write( reinterpret_cast<char*>(&new_data[nvarnew*i]), blocksize );
offs.write( reinterpret_cast<char*>(&blocksize), sizeof(int) );
}
offs.close();
and here I how I try to attempt to reading it with Fortran
subroutine read_output(filename)
implicit none
character (*),intent (in) :: filename
integer(4)::temp,temp2
real(4)::tempr
real(kind=8),allocatable,dimension(:)::dx,xgas,ygas,zgas,nH,divv
real(kind=8),allocatable,dimension(:)::force_dm_x,force_dm_y,force_dm_z
real(kind=8),allocatable,dimension(:)::force_gas_x,force_gas_y,force_gas_z
real(kind=8),allocatable,dimension(:)::force_stars_x,force_stars_y,force_stars_z
integer::Nmax,Ngas2
Nmax = 1000000
allocate(dx(Nmax),xgas(Nmax),ygas(Nmax),zgas(Nmax),nH(Nmax),divv(Nmax))
allocate(force_dm_x(Nmax),force_dm_y(Nmax),force_dm_z(Nmax))
allocate(force_gas_x(Nmax),force_gas_y(Nmax),force_gas_z(Nmax))
allocate(force_stars_x(Nmax),force_stars_y(Nmax),force_stars_z(Nmax))
open ( 19 , file = filename, form = 'unformatted',access='stream')
Ngas2=1
DO WHILE (Ngas2.lt.Nmax)
read (19,end=6) temp,dx(Ngas2),xgas(Ngas2),ygas(Ngas2),zgas(Ngas2),nH(Ngas2),divv(Ngas2),&
& force_dm_x(Ngas2),force_dm_y(Ngas2),force_dm_z(Ngas2),&
& force_gas_x(Ngas2),force_gas_y(Ngas2),force_gas_z(Ngas2),&
& force_stars_x(Ngas2),force_stars_y(Ngas2),force_stars_z(Ngas2)
read(19) temp
if(Ngas2==1) then
write(*,*) dx(Ngas2),xgas(Ngas2),ygas(Ngas2),zgas(Ngas2),nH(Ngas2),divv(Ngas2),&
& force_dm_x(Ngas2),force_dm_y(Ngas2),force_dm_z(Ngas2),&
& force_gas_x(Ngas2),force_gas_y(Ngas2),force_gas_z(Ngas2),&
& force_stars_x(Ngas2),force_stars_y(Ngas2),force_stars_z(Ngas2)
end if
Ngas2=Ngas2+1
end do
6 continue
close (19)
if( Ngas2.eq.Nmax ) then
print *, 'DATA GAS - error reading data: too many cells. Change Nmax.'
print *, 'Ngas=',Ngas2
stop
end if
Ngas2=Ngas2-1
! print *,Ngas
!print*,dx(1),xgas2(1),ygas2(1),zgas2(1),nH(1),divv(1),vortx(1),vorty(1),vortz(1),baroc(1),vortentr(1),vortrho(1),strain(1),strainpar(1),strainper(1),qp(1),qp_per(1),qp_par(1),kin(1),gforce(1),qphi(1),qphi_par(1),qphi_per(1),gx(1),gy(1),gz(1),px(1),py(1),pz(1),sx(1),sy(1),sz(1),phi(1)
end subroutine read_output
program main
implicit none
call read_output("binary_c_code.dat")
end program main
But the problem is that when I try to print what I read I get numbers that don't really make sense.
I guess the problem might be in the declaration of the variables. I even tried declaring them as real(kind=4)
, but I still get numbers that don't make sense.