M.E.
M.E.

Reputation: 5515

How to detect end of data in standard input in Fortran 95?

I am trying to read a CSV dataset in Fortran 95.

I have originally asked this question which has been marked as duplicate, two posts have been suggested. However, when I incorporate the posts check for iostat I still get Segmentation Overflow. The posts are related to read a file while my problem is related to standard input (that might not be fully clear in the original SO request, so I make it more clear now).

The CSV data has the following structure:

12,30,2010,23,00,01,125550,1,10643,125550,125575,4665142,0,0
12,30,2010,23,00,44,125550,1,10644,125525,125550,4665188,0,0
12,30,2010,23,01,55,125575,1,10645,125550,125575,4665287,0,0
12,30,2010,23,02,20,125550,1,10646,125550,125575,4665299,0,0

The data is presented in the standard input.

I have been suggested to look at these SO posts:

Using do loop in a Fortran 90 program to read different number of lines for n frames?

Read a file with an unknown number rows in Fortran

Both uses files, not the standard input.

I have modified my code and added the iostat check:

program file_parser

        implicit none

        ! -------------------
        ! TYPE DEFINITION
        ! -------------------
        type :: type1_record
                integer :: month
                integer :: day
                integer :: year
                integer :: hour
                integer :: minute
                integer :: second
                integer :: field1
                integer :: field2
                integer :: field3
                integer :: field4
                integer :: field5
                integer :: field6
                integer :: field7
                integer :: field8
        end type

        ! -------------------
        ! VARIABLE DEFINITION
        ! -------------------
        integer :: i, io_result
        type(type1_record), dimension(10000) :: input_data
        i = 0
        do 
                read(*,*,iostat=io_result) input_data(i)
                if (io_result /= 0) exit
                i = i + 1        
        end do

        do i = 1, 3
                write(*,*) input_data(i)
        end do


end program

However, when I run the program I still get Segmentation fault (core dumped):

$cat test_data.txt | ./a.out
       12           30         2010           23            0           44 
   125550            1        10644       125525       125550      4665188 
        0            0
       12           30         2010           23            1           55 
   125575            1        10645       125550       125575      4665287 
        0            0
       12           30         2010           23            2           20 
   125550            1        10646       125550       125575      4665299 
        0            0
Segmentation fault (core dumped)

Note: test_data.txt contains the data presented at the beginning.

What I am doing wrong with respect of the SO posts that have been previously suggested?

Edit: Apparently it reads the standard input until end of data and then outputs the three registers, but the program ends with Segmentation fault.

Upvotes: 0

Views: 122

Answers (0)

Related Questions