Reading MS Excel Files in FORTRAN 77

Click For Summary

Discussion Overview

The discussion revolves around reading MS Excel created CSV or tab-delimited files in FORTRAN 77. Participants share code snippets, error messages, and troubleshooting tips related to file reading issues.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Experimental/applied

Main Points Raised

  • Ekin seeks assistance on reading CSV or tab-delimited files in FORTRAN 77.
  • Mathmate provides a code snippet for reading a text file, suggesting renaming the file to Gucu.txt.
  • Ign reports an "End of file" error when attempting to read the file using Mathmate's code.
  • A later reply from Mathmate suggests verifying the content of the text file for extra characters or missing newlines.
  • Ign mentions that the provided data was missing lines and shares a new file format they are trying to read.
  • Another participant successfully runs a modified program with Ign's data, suggesting shortening the file name might help.
  • Ign later reports success after saving the file as UTF-8 without a .txt extension, though they express uncertainty about the cause.
  • A participant speculates that gfortran's default encoding might be incompatible with Ubuntu's native code and suggests using native editors for file creation.
  • A new participant inquires about reading data in FORTRAN 90-95 and seeks advice on creating a specific array structure.

Areas of Agreement / Disagreement

The discussion includes multiple competing views and unresolved issues regarding file reading methods and potential encoding problems. Participants share different experiences and solutions without reaching a consensus on a single approach.

Contextual Notes

Participants mention issues related to file content, encoding, and compatibility with different FORTRAN versions, but these remain unresolved and context-dependent.

Who May Find This Useful

This discussion may be useful for programmers working with FORTRAN who need to read data from CSV or text files, particularly those facing similar encoding or file format issues.

ekinakoglu
Messages
1
Reaction score
0
Dear Members,

How can I read an MS Excel created csv or tab delimited file in FORTRAN 77? The two tab delimited and csv files are attached.

Thank you,

Ekin
 

Attachments

Technology news on Phys.org
Rename the file to Gucu.txt and try the following code:
Code:
      INTEGER NLINES, NVALUES
      PARAMETER(NLINES=7,NVALUES=5)
      REAL*8 VAL(NLINES,NVALUES)
      OPEN(1,FILE='GUCU.TXT')
      DO 5 I=1,NLINES
    5 READ(1,*)(VAL(I,J),J=1,NVALUES)
      DO 15 I=1,NLINES
   15 WRITE(6,'(5F10.3)')(VAL(I,J),J=1,NVALUES)
      STOP
      END
 
Hello, I am also trying to read from a csv or txt file in Fortran and
I have tried the commands that "mathmate" provided for the Gucu.txt
(using "ekinakoglu" 's file).
but i got this error :
> At line 10 of file readexcel.f90
> Fortran runtime error: End of file

line 10 is: 5 READ(1,*)(VAL(I,J),J=1,NVALUES)

can anyone help me on this?

thank you in advance

Ign
 
I just tested again the execution of the above program and everything works fine.
You may want to verify the content of the given text tile gucu.txt, shown below:
Code:
0.1414607   2.1 12  0.9 0.175
1.607   10  39.2    0   0.255102
35  32  137 0.03990063  0.2335766
8.806   325 -999    0.8399166   -999
-999    -999    -999    0.9591094   -999
Sometimes there are extra characters introduced when you download a file, or a newline missing. You can compare the contents. If all else fails, add a blank line after the last, and delete it. This will ensure you have a newline of your system.
Again, if it does not work, try printing what you read.
 
hello, thank you for the reply

i still get the same error
>At line 7 of file readexcel.f90
>Fortran runtime error: End of file

by the zay the data you pasted was missing two lines I believe

anyway the file I want the fortran program to read looks like this:
[stationID and feature]

A2332110 PLUVI
A2352020 PLUVI
A3301010 NIVAL
A3422010 PLUVI
A3792010 TRANS
A3832010 PLUVI

I have tried to modify the commands you provided for the old file
to work with this new one but I get an error back ! thank you in advance

program readfile
INTEGER NLINES, NVALUES
PARAMETER(NLINES=6,NVALUES=2)
CHARACTER*8 VAL(NLINES,NVALUES)
OPEN(1,FILE='stationvsregimeetiage04june09.TXT')
DO 5 I=1,NLINES
5 READ(1,*)(VAL(I,J),J=1,NVALUES) ! Line 8
DO 15 I=1,NLINES
15 WRITE(6,*)(VAL(I,J),J=1,NVALUES)
STOP
END

At line 8 of file readfile.f90
Fortran runtime error: End of file
 
I picked up your data and ran the following program. The results print correctly.
Code:
      INTEGER NLINES, NVALUES
      PARAMETER(NLINES=6,NVALUES=2)
      CHARACTER*8 VAL(NLINES,NVALUES)
      OPEN(1,FILE='CSV.DAT')
      DO 5 I=1,NLINES
    5 READ(1,*)(VAL(I,J),J=1,NVALUES)
      DO 15 I=1,NLINES
   15 WRITE(6,'(5A10)')(VAL(I,J),J=1,NVALUES)
      STOP
      END
Results:
Code:
 A2332110  PLUVI
 A2352020  PLUVI
 A3301010  NIVAL
 A3422010  PLUVI
 A3792010  TRANS
 A3832010  PLUVI

Perhaps you could shorten the file name to 8 characters and see if it helps.
 
Last edited:
it eventually worked, thank you

I simply saved the same file as text UTF-8 with no .txt extension and it worked great
I don't know if it's related to the compiler (gfortran) or the system (linux ubuntu), no idea

ig
 
It may be that gfortran defaults to utf-8 which is not compatible with the native code for ubuntu. You may want to do some tests with text files created in ubuntu using native editors such as EMACS or vi.

But I'm glad it all works out.
 
Hi!

I am trying to open the attached xls or txt file with fortran 90-95.
Firstly, is the data in the correct position?
Is the previous code applicable to read and process my data?
My pusrpose is to create an array 20*20 with total 400 numbers in each cell.
For any other suggestion of other codes I will be really grateful.

Thanks for the attention.
 
  • #10
The files are here..
 

Attachments

Similar threads

  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 22 ·
Replies
22
Views
4K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 19 ·
Replies
19
Views
2K
  • · Replies 14 ·
Replies
14
Views
7K
  • · Replies 5 ·
Replies
5
Views
5K
  • · Replies 12 ·
Replies
12
Views
3K
Replies
19
Views
4K