[Fortran] problem on reading the csv file

  • Fortran
  • Thread starter fortranuser
  • Start date
  • #1

Main Question or Discussion Point

hi.
I have a csv file name Relative which has 24 different data set containing characters as well as numbers. It looks like
Date,Hour,Min (RH),Max (RH),Avg (RH)

2012-08-28,0,85.190,87.200,86.325

2012-08-28,1,85.390,88.570,86.775

With reference to one of the thread reply in this forum ,I wrote a fortran code to read the file which is as follows

PROGRAM CSVREAD
CHARACTER(LEN=11)::DATE
INTEGER::HR
REAL::MAX,MIN,AVG
OPEN(FILE="RELATIVE",UNIT=15,STATUS="UNKNOWN")

DO WHILE(.TRUE.)

READ(15,*,END=123)DATE,HR,MIN,MAX,AVG
WRITE(*,*)DATE,HR,MIN,MAX,AVG
END DO
123 CONTINUE
CLOSE(15)
END PROGRAM CSVREAD

This program was successfully compiled but while executing its exe file no output in the terminal.
 

Answers and Replies

  • #2


In above program I found out 1 mistake . The file to be opened was Relative instead RELATIVE. Again on executing exe file i got error message .

At line 9 of file csvread.f90 (unit = 15, file = 'Relative')
Fortran runtime error: Bad integer for item 1 in list input

Again I change code to
Read(15,*,END=123) DATE
write(*,*)DATE

That case i got 24 different set of date.
But on including Date,Hour,Min (RH),Max (RH),Avg (RH)
the problem is the same
 
Last edited:
  • #3
1,065
53
The program works just fine after removing usage of input file (opening, reading from unit 15 and closing).

I personally don't use an explicit file unit for input files, because then I am stuck with a given file name; instead, I use re-direction...so, I simply read from the implicit standard input.

This works for me:
Code:
PROGRAM CSVREAD
CHARACTER(LEN=11)::DATE
INTEGER::HR
REAL::MAX,MIN,AVG

DO WHILE(.TRUE.)
    READ(*,*,END=123)DATE,HR,MIN,MAX,AVG
    WRITE(*,*)DATE,HR,MIN,MAX,AVG
END DO
123 CONTINUE

END PROGRAM CSVREAD
Then, execute progra like this:
Code:
> readcsv < Relative
Or whatever is your actual fortran file's name and your csv file's name...and yes, pay attention to letter's case....Windows exercises case-preserving for you, but IT IS NOT case sensitive; Unix/Linux, on the other hand ARE case sensitive.
 
  • #4
The program works just fine after removing usage of input file (opening, reading from unit 15 and closing).

I personally don't use an explicit file unit for input files, because then I am stuck with a given file name; instead, I use re-direction...so, I simply read from the implicit standard input.

This works for me:
Code:
PROGRAM CSVREAD
CHARACTER(LEN=11)::DATE
INTEGER::HR
REAL::MAX,MIN,AVG

DO WHILE(.TRUE.)
    READ(*,*,END=123)DATE,HR,MIN,MAX,AVG
    WRITE(*,*)DATE,HR,MIN,MAX,AVG
END DO
123 CONTINUE

END PROGRAM CSVREAD
Then, execute progra like this:
Code:
> readcsv < Relative
Or whatever is your actual fortran file's name and your csv file's name...and yes, pay attention to letter's case....Windows exercises case-preserving for you, but IT IS NOT case sensitive; Unix/Linux, on the other hand ARE case sensitive.

Hi gsal.Thank you.
I have saved the fortran code in csvread.f90 and csv filename is Relative.
To compile , in terminal I have to write gfortran -o csvread.exe csvread.f90
To execute it ,in terminal I write as ./csvread.exe

removing usage of input file (opening, reading from unit 15 and closing) didnt work in my case.
 
  • #5
1,065
53
go ahead and paste a copy of what the terminal looks like from compiling the program and running it or screen shot if you cannot simply copy the text
 
  • #6
go ahead and paste a copy of what the terminal looks like from compiling the program and running it or screen shot if you cannot simply copy the text
Hi,
I have attached the screenshot of my terminal used in centos 6. For your information I use vi editor to write fortran program.
 

Attachments

  • #7
1,065
53
Hhhhmmm...

The error message says that there is a type discrepancy...while the program is expecting an integer, the number found in the input file (at some given time) does not seem to be one.

Go ahead and add a debugging write statement after the read to print back to the screen what you just read; also, follow that write statement with a flush....we don't want the program to terminate and leaving stuff in the queue...we want all of it back so we know exactly which was the last line read from the input file.

Also, look into compilation flags that the gfortran compile takes....add whatever helps, like flags the report more stuff at run time, or flags for debugging purposes, tracing, etc.

Is what you are presenting your actual entire program? or is your program larger than this with other read statements somewhere else? reading something else from the Relative file, etc?...because that way I see, the error does not quite make sense or something...unless the message is not 100% precise...we shall see.

Please attach both your latest source code and your actual Relative file. I say attach, because I don't want you copy and paste and possibly eliminate characters from the actual file, etc.

...so, please attach, if you still can't make the program run after reviewing it one more time on the basis of the above hints.
 

Related Threads for: [Fortran] problem on reading the csv file

  • Last Post
Replies
2
Views
2K
Replies
2
Views
648
Replies
5
Views
3K
Replies
1
Views
15K
Replies
9
Views
18K
  • Last Post
Replies
2
Views
2K
Replies
7
Views
1K
Replies
1
Views
2K
Top