Fortran [Fortran] problem on reading the csv file

AI Thread Summary
The discussion revolves around troubleshooting a Fortran program designed to read a CSV file containing date and numerical data. The user initially faced issues with the program not producing output, which was traced back to a case sensitivity error in the file name. After correcting this, a runtime error occurred due to a type mismatch, indicating that the program expected an integer but encountered a different data type. Suggestions included simplifying the input method by using standard input redirection instead of explicit file handling, which resolved some issues for another participant. The user was advised to add debugging statements to identify the last successfully read line and to explore compilation flags for better error reporting. There was also a request for the user to share their terminal output and the contents of the CSV file for further assistance. The discussion emphasizes the importance of careful attention to data types and file handling in Fortran programming.
fortranuser
Messages
7
Reaction score
0
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.
 
Technology news on Phys.org


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:
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.
 
gsal said:
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.
 
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
 
gsal said:
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

  • Screenshot-3.png
    Screenshot-3.png
    16.4 KB · Views: 1,007
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.
 

Similar threads

Replies
8
Views
1K
Replies
12
Views
3K
Replies
5
Views
5K
Replies
5
Views
2K
Replies
1
Views
3K
Replies
8
Views
4K
Replies
2
Views
2K
Replies
10
Views
4K
Replies
1
Views
2K
Back
Top