Home > Archive > Fortran > July 2004 > unformatted 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]
|
|
|
| I'm trying to write some byte into a unformatted file (binary) but I have
some problems. For example if I want to write an integer of 4 bytes what can
I do? now I do this:
integer :: number
character*1 :: buffer(filesize)
....
do i=1,4
buffer(i)=char(number)
j=j+1
enddo
....
open(1,file=filename,status='unknown',fo
rm='unformatted', &
access='direct',recl=filesize)
write(1,rec=1) (buffer(j),j=1,filesize)
close(1)
if(allocated(buffer)) deallocate(buffer)
the number is writed but is replicated for 4 times. Insted I want this: xx
00 00 00
Where can I find a good tutorial for fortran woth large examples about
"files"? In my fortran book there are only sequential files!
| |
| Jan Vorbrüggen 2004-07-28, 9:06 pm |
| The main thing about unformatted files is that they are basically a
memory dump of whatever you want to put there. So all you need to do
is
write (1, rec=1) number
in your exmple. This is not C, you know.
Then there is the thing about records. But if you just want to write
some variables and arrays to a file, and read them back in exactly
the same way later, you don't even need to know about records.
Jan
| |
| Dr Ivan D. Reid 2004-07-28, 9:06 pm |
| On Tue, 27 Jul 2004 09:27:12 +0200, rand <joleg@noMAIL.it>
wrote in <GsnNc.4988$B06.762@news.edisontel.com>:
> I'm trying to write some byte into a unformatted file (binary) but I have
> some problems. For example if I want to write an integer of 4 bytes what can
> I do? now I do this:
> integer :: number
> character*1 :: buffer(filesize)
> ...
> do i=1,4
> buffer(i)=char(number)
^^^^^^^^^^^^^^^^^^^^^^
> j=j+1
> enddo
> ...
> open(1,file=filename,status='unknown',fo
rm='unformatted', &
> access='direct',recl=filesize)
> write(1,rec=1) (buffer(j),j=1,filesize)
> close(1)
> if(allocated(buffer)) deallocate(buffer)
> the number is writed but is replicated for 4 times. Insted I want this: xx
> 00 00 00
Yes, take a look at the part I highlighted -- it's loop-invariant
and will do the same thing four times. Do you want to write the ASCII
representation of your integer into the file? For that you could use
an "internal write" into your intermediate storage, e.g.
character*4 :I ibuf
....
write(ibuf,'(I4.4)')number
....
> Where can I find a good tutorial for fortran woth large examples about
> "files"? In my fortran book there are only sequential files!
--
Ivan Reid, Electronic & Computer Engineering, ___ CMS Collaboration,
Brunel University. Ivan.Reid@brunel.ac.uk Room 40-1-B12, CERN
KotPT -- "for stupidity above and beyond the call of duty".
|
|
|
|
|