[Fortran] problem on reading the csv file

Click For Summary

Discussion Overview

The discussion revolves around a problem encountered while reading a CSV file using Fortran. Participants are exploring issues related to file handling, data types, and debugging techniques in the context of programming and data input/output operations.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant describes their Fortran code for reading a CSV file but reports no output during execution.
  • Another participant identifies a potential mistake in the file name casing and notes a runtime error related to data type mismatches when attempting to read multiple variables.
  • A different participant suggests an alternative approach by using standard input instead of explicit file handling, which they claim works for them.
  • One participant expresses that removing file handling did not resolve their issue, indicating a possible misunderstanding or different context of use.
  • Another participant points out a type discrepancy error and recommends adding debugging statements to identify the last successfully read line from the input file.
  • There is a request for the participant to share terminal output or screenshots to better understand the issue.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the best approach to resolve the issue, and multiple competing views on file handling and debugging techniques remain present throughout the discussion.

Contextual Notes

There are unresolved issues related to data type discrepancies and the specific content of the CSV file, which may affect the program's execution. The discussion also highlights potential limitations in the participants' understanding of file handling in Fortran.

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,039
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 ·
Replies
8
Views
2K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 5 ·
Replies
5
Views
5K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 10 ·
Replies
10
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K