|
|
Up |
|
|
  |
Author: RaguRagu Date: Sep 10, 2008 13:20
I have a simple question regarding allocating a character array that
can represent a line in an input file. I am writing a small function
that can read a line from an input file, strip the Fortran 90 style
comments (Exclamation mark) and return the string.
Currently, I had fixed the output string to be of len = 1000. So, the
function that I had written is only limited to 1000 characters. I
want the returned string’s length to be dynamic and equal to the
actual length of the line in the input file (minus comments).
Something like TRIM(input_file_length). Could some point me to a way
of doing this ?
!-------------------------------------------------------------------------------
! nextline(funit) -- defaulted to public
! Return the next line from a file unit without any Fortran 90 style
comments.
function nextline ( funit ) result ( outstring )
use, intrinsic :: ISO_FORTRAN_ENV, only: OUTPUT_UNIT
implicit none
|
| Show full article (3.15Kb) |
|
| | 19 Comments |
|
  |
Author: nospamnospam Date: Sep 10, 2008 14:20
Ragu gmail.com> wrote:
> I have a simple question regarding allocating a character array that
> can represent a line in an input file.
First, please note that you are *NOT* talking about an array. A string
and an array are different things. You can have an array of characters,
but that's not the usual Fortran idiom (and thus a lot of string
intrinsics and other things won't work or won't do what you might want).
What you have is a scalar (not array) Fortran string. Yes, it makes a
difference - quite a big one as it tirns out.
That being said...
> I
> want the returned string's length to be dynamic and equal to the
> actual length of the line in the input file (minus comments).
> Something like TRIM(input_file_length). Could some point me to a way
> of doing this ?
There are two separate problems here. One is solvable with a bit of
work. The other really needs f2003.
The one that is solvable with a bit of work is that you need to read the
line in first in order to find out how long it is. That means that you
can't read directly into the character variable in question...
|
| Show full article (4.31Kb) |
|
| | no comments |
|
  |
Author: GaryScottGaryScott Date: Sep 10, 2008 14:38
On Sep 10, 4:20Â pm, nos...@see.signature (Richard Maine) wrote:
> Ragu gmail.com> wrote:
>
> The one that really needs f2003 is dynamic allocation of the length of a
> character string. In f2003, that's pretty straightforward. You just make
> the character allocatable length and use an allocate statement (or an
> assignment, which allocates as needed). But f95 doesn't have allocatable
> character length, and that doesn't appear to be one of the f2003
> features that is appearing in f95 compilers yet.
Was curious as to how allocatable strings are likely implemented. A
derived type with a length component and a string content component?
Anything else (additional components)?
>
> --
> Richard Maine           | Good judgement comes from experience;
> email: last name at domain . net | experience comes from bad judgement.
> domain: summertriangle      |  -- Mark Twain
|
| |
| no comments |
|
  |
Author: RaguRagu Date: Sep 10, 2008 14:54
On Sep 10, 5:20Â pm, nos...@see.signature (Richard Maine) wrote:
> Ragu gmail.com> wrote:
>> I have a simple question regarding allocating a character array that
>> can represent a line in an input file.
>
> First, please note that you are *NOT* talking about an array. A string
> and an array are different things. You can have an array of characters,
> but that's not the usual Fortran idiom (and thus a lot of string
> intrinsics and other things won't work or won't do what you might want).
> What you have is a scalar (not array) Fortran string. Yes, it makes a
> difference - quite a big one as it tirns out.
>
> That being said...
>
>> I
>> want the returned string's length to be dynamic and equal to the
>> actual length of the line in the input file (minus comments).
>> Something like TRIM(input_file_length). Could some point me to a way
>> of doing this ?
> ...
|
| Show full article (2.43Kb) |
| no comments |
|
  |
Author: nospamnospam Date: Sep 10, 2008 15:09
Ragu gmail.com> wrote:
> I do not want to go into expanding buffers. I can live with increasing
> the default length of string from 1,000 characters to say 10,000
> characters. However, I need to find a way to abort or stop if a line
> continues beyond my starting string length (10,000).
>
> Is there a way to check whether the line read from the input file is
> less than the length of the string used in the reading process
> (something like looking for a string termination character). Each line
> in a text file should be a record right? So is there a way to find the
> end of the record?
"String termination character" isn't the right concept here. Fortran
doesn't have one. In fact, it would be a contradiction in terms in
Fortran. If it was a character then it could be in a string, and not
necessarily at the end. Neither do all record structures.
See the size= specifier for the READ statement. You'll have to be doing
nonadvancing I/O for that, but that's not a particularly big deal. Some
compilers have related nonportable things (I think the Q edit
descriptor, or something like that).
|
| Show full article (2.34Kb) |
| no comments |
|
  |
Author: Sjouke BurrySjouke Burry Date: Sep 10, 2008 15:59
Ragu wrote:
> On Sep 10, 5:20 pm, nos...@see.signature (Richard Maine) wrote:
>> Ragu gmail.com> wrote:
>>> I have a simple question regarding allocating a character array that
>>> can represent a line in an input file.
>> First, please note that you are *NOT* talking about an array. A string
>> and an array are different things. You can have an array of characters,
>> but that's not the usual Fortran idiom (and thus a lot of string
>> intrinsics and other things won't work or won't do what you might want).
>> What you have is a scalar (not array) Fortran string. Yes, it makes a
>> difference - quite a big one as it tirns out.
>>
>> That being said...
>>
>>> I
>>> want the returned string's length to be dynamic and equal to the
>>> actual length of the line in the input file (minus comments).
>>> Something like TRIM(input_file_length). Could some point me to a way
>>> of doing this ?
>> There are two separate problems here. One is solvable with a bit of ...
|
| Show full article (2.69Kb) |
| no comments |
|
  |
Author: nospamnospam Date: Sep 10, 2008 17:07
GaryScott sbcglobal.net> wrote:
> Was curious as to how allocatable strings are likely implemented. A
> derived type with a length component and a string content component?
> Anything else (additional components)?
I think you are confusing a few things here.
First, they certainly aren't implemented as a derived type, insomuch as
that's a Fortran thing and there aren't currently any Fortran compilers
written in Fortran. (There have been in the past, and I'd think f2003 a
suitable language to write one in today, in fact that's an idea I've
toyed with, but there aren't any right now).
Second, the answer depends partly on context. Fundamentally, an
allocatable string needs a pointer to the dynamically allocated body and
an indication of the length. For an allocatable string standing...
|
| Show full article (2.17Kb) |
| no comments |
|
  |
Author: George McBaneGeorge McBane Date: Sep 10, 2008 18:20
Richard Maine wrote:
>
> Yes. Heck, it wasnt too awfully long ago that Fortran had *NO* dynamic
> allocation of any kind. Dynamic allocation wasn't standard in Fortran
> until f90. Prior to that, some codes used nonstandard nonportable hacks
> of various kinds.
I work regularly with a legacy F77 program that has author-written
DMA. (I'm not the author.)
The program structure permits a stack sort of allocation.
At the beginning you allocate one humongous array...
|
| Show full article (1.48Kb) |
| no comments |
|
  |
Author: Gary ScottGary Scott Date: Sep 10, 2008 18:56
George McBane wrote:
> Richard Maine wrote:
>
>>
>> Yes. Heck, it wasnt too awfully long ago that Fortran had *NO* dynamic
>> allocation of any kind. Dynamic allocation wasn't standard in Fortran
>> until f90. Prior to that...
|
| Show full article (1.99Kb) |
| no comments |
|
  |
|
|
  |
Author: nospamnospam Date: Sep 10, 2008 18:58
George McBane gvsu.edu> wrote:
> Richard Maine wrote:
>
>>
>> Yes. Heck, it wasnt too awfully long ago that Fortran had *NO* dynamic
>> allocation of any kind. Dynamic allocation wasn't standard in Fortran
>> until f90. Prior to that, some codes used nonstandard nonportable hacks
>> of various kinds.
...
> At the beginning you allocate one humongous array X, and
> put the array and an integer pointer to the "next available element"
> in common.
...
> The reason I'm writing this: this simple scheme IS portable.
> I've used it on a dozen architectures with almost as many
> Fortran compilers and it has never caused trouble. So for a limited
> class of needs, there was a portable way to do allocations.
|
| Show full article (2.36Kb) |
| no comments |
|
RELATED THREADS |
  |
|
|
|
|
|