For Programmers: Free Programming Magazines  


Home > Archive > Fortran > January 2008 > creating binary direct access file









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author creating binary direct access file
gislita@gmail.com

2008-01-12, 7:14 pm

Hi. I am really new to fortran. I have an excel file with a column of
data. I would need to create with fortran a binary direct access file
containing the data in the mentioned excel file. I have been told that
this is really easy, but I just don't know how to do it. Any help will
be highly appreciated!
Thank you.
e p chandler

2008-01-12, 7:14 pm

On Jan 12, 1:34=A0pm, gisl...@gmail.com wrote:
> Hi. I am really new to fortran. I have an excel file with a column of
> data. I would need to create with fortran a binary direct access file
> containing the data in the mentioned excel file. I have been told that
> this is really easy, but I just don't know how to do it. Any help will
> be highly appreciated!
> Thank you.


1. There is no need to do something so complicated.

Simply write a text file from Fortran.

implicit none
integer i

open(10,file=3D'foo.csv')

do i=3D1,10
write(10,*) i
end do

end

Double click on the file and there it is in Excel.

2. Look up CSV format.

3. Do your own homework!

-- e

gislita@gmail.com

2008-01-14, 4:27 am

On 12 Jan, 19:38, e p chandler <e...@juno.com> wrote:
> On Jan 12, 1:34=A0pm, gisl...@gmail.com wrote:
>
>
> 1. There is no need to do something so complicated.
>
> Simply write a text file from Fortran.
>
> implicit none
> integer i
>
> open(10,file=3D'foo.csv')
>
> do i=3D1,10
> =A0 write(10,*) i
> end do
>
> end
>
> Double click on the file and there it is in Excel.
>
> 2. Look up CSV format.
>
> 3. Do your own homework!
>
> -- e


Thank you very much for your answer. I am trying that, but everytime I
run fortran with that code I get an error, telling me that there is a
non-numeric character and it doesn't do it... I don't know why it
doesn't recognise the values in the csv file as numeric. Thank you for
your help.
Arjen Markus

2008-01-14, 8:12 am

On 14 jan, 09:50, gisl...@gmail.com wrote:
> On 12 Jan, 19:38, e p chandler <e...@juno.com> wrote:
>
>
>
>
>
>
[color=darkred]
[color=darkred]
>
>
>
>
>
>
>
>
>
>
>
> Thank you very much for your answer. I am trying that, but everytime I
> run fortran with that code I get an error, telling me that there is a
> non-numeric character and it doesn't do it... I don't know why it
> doesn't recognise the values in the csv file as numeric. Thank you for
> your help.- Tekst uit oorspronkelijk bericht niet weergeven -
>
> - Tekst uit oorspronkelijk bericht weergeven -


You will have to show the code you are using and what the
exact error message is. (Helpful too: operating system and
compiler).

Is the Fortran program complaining or is Excel complaining?
It is not clear (to me) from your description which one.
meek@skyway.usask.ca

2008-01-14, 8:12 am

In a previous article, gislita@gmail.com wrote:
>Hi. I am really new to fortran. I have an excel file with a column of
>data. I would need to create with fortran a binary direct access file
>containing the data in the mentioned excel file. I have been told that
>this is really easy, but I just don't know how to do it. Any help will
>be highly appreciated!
>Thank you.

One way might be to export the data from exel as csv
(in which all numbers are separated by commas?), then read
from Fortran as
Parameter (ncols= ...)
dimension acolmn(ncols)
open(20,file='foo.csv',form='formatted')
read(20,*)acolmn
gislita@gmail.com

2008-01-14, 7:19 pm

On Jan 14, 1:53=A0pm, m...@skyway.usask.ca wrote:
> In a previous article, gisl...@gmail.com wrote:>Hi. I am really new to for=

tran. I have an excel file with a column of
>
> =A0One way might be to export the data from exel as csv
> (in which all numbers are separated by commas?), then read
> from Fortran as
> =A0 Parameter (ncols=3D ...)
> =A0 dimension acolmn(ncols)
> =A0 open(20,file=3D'foo.csv',form=3D'formatted')
> =A0 read(20,*)acolmn


Thank you for your answers. I don't know if I am explaining myself
properly. I am trying to create a binary direct access file (*.dat
file) with gfortran. I have already an excel file (or csv, or text
file, I can change that to whatever) with some numerical values. When
I try to run fortran with any of the two examples above (from e p
chandler or from m...@skyway.usask.ca) I always get the same answer,
something like (it is in Spanish so I am translating)

"error: Non-numeric character in label field at (1)
in the *.F file

error: unclassified declaration at (1)
in the *.F file"

Sorry I am badgering you so much with this but I just don't know how
to make it work!

Thank you
glen herrmannsfeldt

2008-01-14, 7:19 pm

gislita@gmail.com wrote:

(snip)

> Thank you for your answers. I don't know if I am explaining myself
> properly. I am trying to create a binary direct access file (*.dat
> file) with gfortran. I have already an excel file (or csv, or text
> file, I can change that to whatever) with some numerical values.


Some of the solutions given are how to write a CSV file, instead
of how to read one.

List directed READ (that is, READ(unit,*)) should be able to
read a CSV file, though it helps to know how many entries
there are, or how many on a line.

To write a direct access file, you need to know the record length
that you want the file to have. You might want to read the CSV
file one line at a time, with a known number of columns, and
write each line as one record of the binary ('UNFORMATTED'
in Fortran) direct access file. Maybe something like:

REAL X(10)
OPEN(UNIT=10,FILE='IN.CSV',STATUS='OLD')
OPEN(UNIT=11,FILE='OUT.DAT',STATUS='REPLACE',
* ACCESS='DIRECT',RECL=40)
DO I=1,1000000
READ(10,*,END=2) X
WRITE(11,REC=I) X
ENDDO
2 STOP
END


G95 seems to expect RECL in bytes, even for a direct access
unformatted file.

One problem with this is that there is no indication in the file
about how many records it contains, and you can't use END=
on a direct access file. Some would write that into the
beginning, something like:

REAL X(10)
OPEN(UNIT=10,FILE='IN.CSV',STATUS='OLD')
OPEN(UNIT=11,FILE='OUT.DAT',STATUS='REPLACE',
* ACCESS='DIRECT',RECL=40)
DO I=2,1000000
READ(10,*,END=2) X
WRITE(11,REC=I) X
ENDDO
2 WRITE(11,REC=1) I-2
END

-- glen

Louis Krupp

2008-01-14, 7:19 pm

gislita@gmail.com wrote:
> On Jan 14, 1:53 pm, m...@skyway.usask.ca wrote:
>
> Thank you for your answers. I don't know if I am explaining myself
> properly. I am trying to create a binary direct access file (*.dat
> file) with gfortran. I have already an excel file (or csv, or text
> file, I can change that to whatever) with some numerical values. When
> I try to run fortran with any of the two examples above (from e p
> chandler or from m...@skyway.usask.ca) I always get the same answer,
> something like (it is in Spanish so I am translating)
>
> "error: Non-numeric character in label field at (1)
> in the *.F file
>
> error: unclassified declaration at (1)
> in the *.F file"
>
> Sorry I am badgering you so much with this but I just don't know how
> to make it work!
>
> Thank you


Change the Fortran file name from something.f to something.f95 and see
if it compiles. It sounds like your problem is the source format; .f
files are assumed to be "fixed format," with numeric statement labels in
columns 1-5 and statements themselves starting in column 7. .f95 files
are assumed to be "free format," which doesn't have those restrictions.

It sounds like you want to read a CSV file and write a direct access
file. Before you do that, you may want to see if you can compile and
run a very simple program; the classic first step is one that says
"Hola mundo" and then exits.

Louis
Terence

2008-01-14, 7:19 pm

With respect to the above posters,
CSV format only uses comma separation for simple cases of one values
per column.
The separator is not limited to commas and tabs are frequently used as
such.

When there are multiple responses (values) within a column (instances)
the column fields are separated by semicolons and the internal values
of a field within on single column, by commas.
The presence of the first semicolon found is a trigger to indicate
that the format of the file is the newer form of CSV format.
Louis Krupp

2008-01-14, 7:19 pm

Louis Krupp wrote:
<snip>
> Change the Fortran file name from something.f to something.f95 and see
> if it compiles.


Oops. Make that something.f90, not .f95. At least one Fortran 95
compiler doesn't recognize the .f95 extension.

Louis
Richard Maine

2008-01-14, 7:19 pm

Terence <tbwright@cantv.net> wrote:

> With respect to the above posters,
> CSV format only uses comma separation for simple cases of one values
> per column.
> The separator is not limited to commas and tabs are frequently used as
> such.
>
> When there are multiple responses (values) within a column (instances)
> the column fields are separated by semicolons and the internal values
> of a field within on single column, by commas.
> The presence of the first semicolon found is a trigger to indicate
> that the format of the file is the newer form of CSV format.


That disagrees with the definition in wikipedia
(<http://en.wikipedia.org/wiki/Comma-separated_values> ), with RFC 4180
(<http://tools.ietf.org/html/rfc4180> ), and with the acronym itself
(Comma-Separated-Values).

Of course, it is quite possible that some particular program like Excel
might put out almost anything and call it a CSV file. In a quick skim
did not find any confirmation of the above description, but I wouldn't
guarantee much.

--
Richard Maine | Good judgement comes from experience;
email: last name at domain . net | experience comes from bad judgement.
domain: summertriangle | -- Mark Twain
Terence

2008-01-14, 7:19 pm

I checked the above definitions and they seem to be the old form of
CSV files.
I checked some Microsoft files and some use the semicolon as
mentioned.

These multiple-instance values fields are used a lot in Market
Research survey data format storage formats and my company software
has to deal with them, read them and write them.

I seem to recall the expression "extended format".

It certainly wasn't a listed option (space, tab, comma) in the version
5 Excel on my local computer, but I loaded a Microsoft CSV file with
that formatting and it was recognised and showed the multiple values
in one column as expected. When I tried to store it (with version 5)
It gave the warning "the current file format is not Microsoft
workbook", but on selecting CSV format, it stored it correctly.
I have to assume that this has become another selectable or default
Microsoft CSV format option in later versions.
Gary Scott

2008-01-14, 10:12 pm

Richard Maine wrote:
> Terence <tbwright@cantv.net> wrote:
>
>
>
>
> That disagrees with the definition in wikipedia
> (<http://en.wikipedia.org/wiki/Comma-separated_values> ), with RFC 4180
> (<http://tools.ietf.org/html/rfc4180> ), and with the acronym itself
> (Comma-Separated-Values).
>
> Of course, it is quite possible that some particular program like Excel
> might put out almost anything and call it a CSV file. In a quick skim
> did not find any confirmation of the above description, but I wouldn't
> guarantee much.
>

comma separated values and tab delimited value formats are separately
treated. If you specify csv, it expects csv as the separator. I often
use tab format however as the markup is simpler.

--

Gary Scott
mailto:garylscott@sbcglobal dot net

Fortran Library: http://www.fortranlib.com

Support the Original G95 Project: http://www.g95.org
-OR-
Support the GNU GFortran Project: http://gcc.gnu.org/fortran/index.html

If you want to do the impossible, don't hire an expert because he knows
it can't be done.

-- Henry Ford
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com