Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

unformatted file
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!



Report this thread to moderator Post Follow-up to this message
Old Post
rand
07-29-04 02:06 AM


Re: unformatted file
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

Report this thread to moderator Post Follow-up to this message
Old Post
Jan Vorbrüggen
07-29-04 02:06 AM


Re: unformatted file
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 c
an
> 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, CER
N
KotPT -- "for stupidity above and beyond the call of duty".

Report this thread to moderator Post Follow-up to this message
Old Post
Dr Ivan D. Reid
07-29-04 02:06 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

Fortran archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 04:30 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.