|
|
Up |
|
|
  |
Author: RadSurferRadSurfer Date: Jul 27, 2008 13:10
I am in need for an important project to write out simple
unsigned-char values in the range $00 through $FF,
without any extraneous data being added:
open: Open the file for "raw 8-bit binary output"
Sequential writes; nothing fancy.
write: Write out individual unsigned 8-bit values with no
extraneous data whatsoever being involved
(a single value results in a single value going to output
file)
close: Close the file with no extraneous data being appended.
Size: File size is only the number of actual values written!
I was told this is quite tricky to do in Fortran, which does seem
silly now; but I wish to learn how it is done.
It would also be helpful to write out an Array of 8-bit values
precisely as in the Array with NO EXTRANEOUS data being
added whatsoever.
Ditto for read. The "block size" will be random, and not a multiple
of 2,4,8, etc...
|
| Show full article (1.12Kb) |
|
| | 26 Comments |
|
  |
Author: Gary ScottGary Scott Date: Jul 27, 2008 13:58
RadSurfer wrote:
> I am in need for an important project to write out simple
> unsigned-char values in the range $00 through $FF,
> without any extraneous data being added:
>
> open: Open the file for "raw 8-bit binary output"
> Sequential writes; nothing fancy.
> write: Write out individual unsigned 8-bit values with no
> extraneous data whatsoever being involved
> (a single value results in a single value going to output
> file)
> close: Close the file with no extraneous data being appended.
> Size: File size is only the number of actual values written!
>
> I was told this is quite tricky to do in Fortran, which does seem
> silly now; but I wish to learn how it is done.
|
| Show full article (1.86Kb) |
|
| | no comments |
|
  |
Author: Thomas KoenigThomas Koenig Date: Jul 27, 2008 15:10
On 2008-07-27, RadSurfer yahoo.com> wrote:
> I am in need for an important project to write out simple
> unsigned-char values in the range $00 through $FF,
> without any extraneous data being added:
program main
integer, parameter :: d1=selected_int_kind(2)
integer(kind=d1), dimension(0:255), parameter :: tr = &
(/(/(i,i=0,127)/),(/(i,i=-128,-1)/)/)
integer, dimension(4) :: x
data x /0, 123, 255, 1/
open(10,form="unformatted",access="stream")
write (10) tr(x)
end program main
$ od -t u1 fort.10
0000000 0 123 255 1
0000004
This does depend in a one-byte type being available with
selected_int_kind(2), though, and uses the F 2003
stream access.
|
| |
| no comments |
|
  |
Author: nospamnospam Date: Jul 27, 2008 15:39
RadSurfer yahoo.com> wrote:
> I am in need for an important project to write out simple
> unsigned-char values in the range $00 through $FF,
> without any extraneous data being added:...
You want the F2003 feature stream I/O. With stream I/O, this is all
pretty straightforward; that's exactly what unformatted stream I/O does.
Just open with access='stream', form='unformatted'. Several f90/f95
compilers have implemented this f2003 feature.
If your compiler doesn't have the f2003 stream I/O feature, it almost
surely does have some simillar functionality, but alas the exact
spelling varies; that's part of why it needed standardizing in f2003.
Although the spelling varies, the functionality is pretty widespread.
If you really want to stick to something that is highly portable to all
f90/f95 (and even f77) compilers with no changes at all, one can use
unformatted direct access, but that's a bit of a pain. It has to use
fixed-size records.You can do your own record management within the
fixed-size blocks, but that can be quite a pain (I well know) and has a
bunch of caveats.
|
| Show full article (1.38Kb) |
| no comments |
|
  |
Author: glen herrmannsfeldtglen herrmannsfeldt Date: Jul 27, 2008 19:23
RadSurfer wrote:
(snip)
> Trying to read in 8-bit raw values from a file in Fortran is strangely
> convoluted apparently.... yet I'm sure it can be done and with
> consistent hopefully portable results.
It is portable as of Fortran 2003. Some Fortran 95 compilers
implement the Fortran 2003 version, as others have indicated.
It sounds easy, but it was considered 'system dependent' for
a long time. From the earliest days of computers, I/O was
record oriented. It turns out that was more efficient for
small memory machines.
With Unix and then DOS/Windows adopting byte oriented file
systems it now seems obvious that Fortran should support
such systems at the low level necessary.
-- glen
|
| |
| no comments |
|
  |
Author: RadSurferRadSurfer Date: Jul 27, 2008 21:38
gfortran (Win32) and CentOS 5.2 (latest release) versions.
What I am finding out is the "implied DO" is not writing out
the same as a full Do/End Do:
open(unit=16,file="BinTest.Bin",iostat=ios,err=100,status="replace",action="write")
! Does not work?
! Write(16,'(A1)',advance="no") (achar(n),n=0,255)
! Writes out an individual 8-Bit Value correctly!
! (and absolutely nothing else!)
! Write(16,'(A1)',advance="no") achar(2)
! Writes out $00 - $FF correctly!
do n=0,255
Write(16,'(A1)',advance="no") achar(n)
end do
close(unit=16)
stop 'AOK'
100 write(*,*) 'ios = ',ios
stop 'Open/Access Error'
On Jul 27, 3:58 pm, Gary Scott sbcglobal.net> wrote:
> RadSurfer wrote:
>> I am in need for an important project to write out simple
>> unsigned-char values in the range $00 through $FF,...
|
| Show full article (1.59Kb) |
| no comments |
|
  |
|
|
  |
Author: James GilesJames Giles Date: Jul 27, 2008 22:24
Ron Ford wrote:
...
>> program main
>> integer, parameter :: d1=selected_int_kind(2)
>> integer(kind=d1), dimension(0:255), parameter :: tr = &
>> (/(/(i,i=0,127)/),(/(i,i=-128,-1)/)/)
>> integer, dimension(4) :: x
>> data x /0, 123, 255, 1/
>> open(10,form="unformatted",access="stream")
>> write (10) tr(x)
>> end program main
...
> I get an unusual file, fort.10, resulting from this that windows
> believes is 4 bytes in size and 4 kilobytes on disk.
|
| Show full article (1.55Kb) |
| no comments |
|
  |
Author: nospamnospam Date: Jul 27, 2008 22:27
RadSurfer yahoo.com> wrote:
> gfortran (Win32) and CentOS 5.2 (latest release) versions.
>
> What I am finding out is the "implied DO" is not writing out
> the same as a full Do/End Do:
That's partly because you are trying to use advance='no' which is *NOT*
one of the methods suggested for what you asked about. Gfortran is bound
to support one of the variants. I suspect it supports F2003 stream I/O.
If not, something like "transparent" or "binary" might work.
But advance='no' has multiple problems in the application you asked for.
First is that it is only for formatted I/O. Formatted I/O isn't
guaranteed to pass all bit patterns unchanged. Unformatted is what you
want, and that doesn't go with advance='no'.
|
| Show full article (2.79Kb) |
| no comments |
|
  |
|
|
  |
Author: nospamnospam Date: Jul 27, 2008 22:27
Ron Ford nowhere.net> wrote:
> I get an unusual file, fort.10, resulting from this that windows believes
> is 4 bytes in size and 4 kilobytes on disk.
That's just the way disk allocation works, which has nothing really to
do with the file contents or size (and pretty much nothing to do with
Fortran). Sounds like a quite ordinary file to me. Now if you managed to
get a file that did *NOT* use a multiple of the disk allocation unit
(apparently 4kB on your system), that would be unusual, indeed.
> I would have thought that the dealbreaker with windows would either be DEL
> or ctrl-Z.
No. Those have nothing to do with unformatted I/O. On any system.
--
Richard Maine | Good judgement comes from experience;
email: last name at domain . net | experience comes from bad judgement.
domain: summertriangle | -- Mark Twain
|
| |
| no comments |
|
|
|
|