Home > Archive > Fortran > June 2007 > read -> type
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 wanna translate a Cpp code to fortran and I am having troubles with
the READ statement.
In Cpp I con do the following (I ll post some code fragments to make
it easier).
I define my own data type:
typedef struct
{
char signam[10];
char sigein[10];
} DATDIR;
Then I define a variable of that type:
DATDIR *DI_ptr;
and now I am reading a files content to that variable:
fread(DI_ptr,sizeof(DATDIR),1,input_file
);
How can I do the same thing using fortran?
Thanks in advance!
| |
| Arjen Markus 2007-06-25, 10:07 pm |
| On 25 jun, 15:01, Nio <nefisc...@gmail.com> wrote:
> I wanna translate a Cpp code to fortran and I am having troubles with
> the READ statement.
>
> In Cpp I con do the following (I ll post some code fragments to make
> it easier).
>
> I define my own data type:
>
> typedef struct
> {
> char signam[10];
> char sigein[10];
>
> } DATDIR;
>
> Then I define a variable of that type:
>
> DATDIR *DI_ptr;
>
> and now I am reading a files content to that variable:
>
> fread(DI_ptr,sizeof(DATDIR),1,input_file
);
>
> How can I do the same thing using fortran?
>
> Thanks in advance!
Open a file as unformatted (or binary - in case you can
use this extension/F2003 feature).
Then:
type(MYTYPE) :: mydata
read(lun) mydata
does the job, unless the type contains pointers/allocatables
Regards,
Arjen
| |
| glen herrmannsfeldt 2007-06-25, 10:07 pm |
| Nio wrote:
> I wanna translate a Cpp code to fortran and I am having troubles with
> the READ statement.
(snip)
> fread(DI_ptr,sizeof(DATDIR),1,input_file
);
> How can I do the same thing using fortran?
Before Fortran 2003, UNFORMATTED files require some type of record
length to be part of the file, either supplied by the file system
or by the Fortran library.
I believe that Fortran 2003 unformatted stream files will do what
you require. OPEN( ..., access='stream', form='unformatted')
Similar features as non-standard extensions are supplied by
some compilers. The Fortran 2003 feature may be supplied by
some Fortran 95 compilers.
-- glen
|
|
|
|
|