Question

i try to convert a input_file.binary in a numbers output_file.txt with a fortran (or if someone have another idea: pyton, java, c++ ...). The ascii file is a 11 rows and N columns file with only numbers (i.e.: 1e-21, 0.0, 1 .... ).

I try this post ASCii to BInary Conversion Program in java but this is a bit different respect to my idea: i want to read the binary and write an output_file.dat so i try with this fortran code:

 program binary
 implicit none

    integer :: i, n
    real, allocatable :: data(:,:)
    open(20,file="input.dat",status="unknown",err=88)
    n = 0
    do
        read(20,*,end=1)
        n = n+1
    end do
    1 rewind(20)
    allocate(data(n,11))
    OPEN(10, file="output_file.txt")
    do i = 1, n
        read(20,*) data(i,:)
        write(10,*) data(i,:)
    end do
    close(20)

 end program binary

when i want to inspect the file with nano file.binary the output is like that:

^@^@^@^@^@^@^@^C^@^@^A�^@^@^@^A^@^@^@^A^@^R�M0wc�6�7�^@^@^@^FA0^@^@^@^@^@^@^@^@^‌​@^@^@^@^@^@^@^@^@^@@�e^A^@^@^@^B+�^[2/[��^@^@^@^FAI�#$

I can compile the program but when i try to run it i have the follow error message:

forrtl: severe (59): list-directed I/O syntax error, unit 20, file /.../input.dat Image PC Routine Line Source ascii 0805D0AA Unknown Unknown Unknown ascii 0805B14D Unknown Unknown Unknown ascii 0804A1EA Unknown Unknown Unknown ascii 08049E67 Unknown Unknown Unknown libc.so.6 B75CF4D3 Unknown Unknown Unknown

Thanks a lot for your help.

Was it helpful?

Solution

You write The ascii file is a 11 rows and N columns and then allocate an array like this:

allocate(data(n,11))

This makes data an n-row, 11-column array, which seems to transpose the dimensions from your file. If you were reading the input file element-by-element that probably wouldn't cause the run-time system to raise an error though it might leave you with the transpose of the array you want.

However, you read the input file row-by-row. As you have explained matters that file should have 11 rows of data but your program tries to read (and write) n rows. Again, there's nothing actually wrong with your program to this point, though it perhaps doesn't do what you expect it to.

But your program expects each row in the input file to provide 11 values and I guess that it doesn't, leading to the error you report.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top