Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

[Fortran] problem on reading the csv file

  1. Aug 29, 2012 #1
    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.
     
  2. jcsd
  3. Aug 30, 2012 #2
    Re: problem on reading the csv file

    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: Aug 30, 2012
  4. Aug 30, 2012 #3
    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 (Text):

    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 (Text):

    > 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.
     
  5. Aug 30, 2012 #4

    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.
     
  6. Aug 30, 2012 #5
    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
     
  7. Aug 31, 2012 #6
    Hi,
    I have attached the screenshot of my terminal used in centos 6. For your information I use vi editor to write fortran program.
     

    Attached Files:

  8. Aug 31, 2012 #7
    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.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: [Fortran] problem on reading the csv file
  1. C++ csv file reading (Replies: 2)

Loading...