Reading MS Excel Files in FORTRAN 77

In summary, the files are csv or tab delimited files and they need to be renamed to Gucu.txt and then tried with the code provided by "mathmate" for the Gucu.txt file. The code works fine when the file has been saved in UTF-8, but it does not work when the file has been saved in a different format.
  • #1
ekinakoglu
1
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

  • files.zip
    490 bytes · Views: 381
Technology news on Phys.org
  • #2
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
 
  • #3
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
 
  • #4
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.
 
  • #5
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
 
  • #6
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:
  • #7
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
 
  • #8
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.
 
  • #9
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

  • data_time.txt
    1.2 KB · Views: 475

What is FORTRAN 77?

FORTRAN 77 (Formula Translation 1977) is a programming language commonly used for scientific and engineering applications. It is a high-level language that allows for efficient and accurate numerical computation.

Why would I need to read MS Excel files in FORTRAN 77?

MS Excel files often contain important data that needs to be processed and analyzed. By reading these files in FORTRAN 77, scientists can use the powerful capabilities of the language to extract and manipulate this data.

How do I read MS Excel files in FORTRAN 77?

There are several ways to read MS Excel files in FORTRAN 77. One method is to use a library such as XL Fortran or ExcelFortran, which provide functions specifically designed for reading and writing Excel files. Another method is to use a converter tool to convert the Excel file into a format that can be read by FORTRAN 77, such as a CSV file.

What are the benefits of reading MS Excel files in FORTRAN 77?

FORTRAN 77 is a powerful programming language that offers a wide range of mathematical and scientific functions. By reading MS Excel files in FORTRAN 77, scientists can take advantage of these functions to perform complex calculations and analysis on their data. Additionally, FORTRAN 77 is a highly efficient language, making it ideal for working with large datasets.

Are there any potential challenges when reading MS Excel files in FORTRAN 77?

One potential challenge is ensuring that the Excel file is formatted correctly for reading in FORTRAN 77. This may require some data manipulation or conversion beforehand. Additionally, some advanced features in newer versions of Excel may not be compatible with FORTRAN 77, so it is important to check for compatibility before attempting to read a file.

Similar threads

  • Programming and Computer Science
Replies
22
Views
3K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
2
Views
875
  • Programming and Computer Science
Replies
14
Views
4K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
19
Views
3K
  • Programming and Computer Science
Replies
8
Views
664
  • Programming and Computer Science
Replies
12
Views
2K
  • Programming and Computer Science
Replies
5
Views
4K
  • Programming and Computer Science
Replies
16
Views
2K
Back
Top