[Fortran] problem on reading the csv file

In summary, 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.
  • #1
fortranuser
7
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
  • #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
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
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.
 
  • #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
 
  • #6
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: 941
  • #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.
 

1. How do I read a CSV file in Fortran?

To read a CSV file in Fortran, you can use the "read" statement with the "formatted" specifier. This will allow you to specify the format of the data in the CSV file and read it into your Fortran program.

2. Why am I getting an error when trying to read a CSV file in Fortran?

There could be several reasons for this error. Some common issues include not specifying the correct file path or name, not properly formatting the data in the CSV file, or not using the correct "read" statement syntax. Double-check these factors and make sure your code is correct.

3. How can I handle missing data in a CSV file when reading it in Fortran?

To handle missing data in a CSV file, you can use the "I/O status" specifier in your "read" statement. This will allow you to check for any errors or missing data while reading the file and handle them accordingly in your code.

4. Can I use Fortran to read a CSV file with a large number of columns?

Yes, Fortran can handle reading CSV files with a large number of columns. However, it is important to be mindful of the format and structure of your data and use appropriate techniques such as arrays to efficiently handle large datasets.

5. Is there a faster way to read a CSV file in Fortran?

Yes, there are several techniques you can use to improve the speed of reading a CSV file in Fortran. These include using the "direct access" specifier in your "read" statement, using parallel processing techniques, and optimizing your code for efficient data handling.

Similar threads

  • Programming and Computer Science
Replies
12
Views
2K
  • Programming and Computer Science
Replies
5
Views
4K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
8
Views
2K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
33
Views
4K
  • Programming and Computer Science
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Programming and Computer Science
Replies
3
Views
2K
Back
Top