For Programmers: Free Programming Magazines  


Home > Archive > Fortran > January 2006 > Fortran 77: Reading empty string









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]

 

Author Fortran 77: Reading empty string
artheartscience

2006-01-31, 7:02 pm

I've written a simple finite automata simulator in Fortran 77, which
works except that I cannot successfully read in and process the empty
string. I've tried the following code, and in my READ statement I
supplied the END=13 which means "if the user just presses enter, goto
line 13". That means just proceed as normal...passing the empty string
to the FA function should work, but I haven't gotten a chance to test
it because although the code compiles using g77, I get a runtime error
which follows my code.

PROGRAM FASimulator

CHARACTER*25 str

10 WRITE(6,11)
11 FORMAT(1x, 'String: ', $)

READ(*, END=13) str

13 IF (FA(str).EQ.1) THEN
PRINT *, 'accepted'
ELSE
PRINT *, 'rejected'
END IF

GOTO 10

STOP
END
-----------------------------
natica% !g
g77 program2.f -o program2
natica% !.
../program2
String: sue: unformatted io not allowed
apparent state: unit 5 (unnamed)
lately reading sequential unformatted external IO
Abort

Michael Metcalf

2006-01-31, 7:02 pm


"artheartscience" <steve@stevedurkin.net> wrote in message
news:1138734730.811956.194080@z14g2000cwz.googlegroups.com...
>
> READ(*, END=13) str
>

Odd, but try using a formatted read for a character variable:

READ(*, fmt='(a)', END=13) str

Regards,

Mike Metcalf


Richard E Maine

2006-01-31, 7:02 pm

artheartscience <steve@stevedurkin.net> wrote:

> in my READ statement I
> supplied the END=13 which means "if the user just presses enter, goto
> line 13".


First, no. It does not mean anything close to that. The end= detects an
end-of file. Pressing the enter key does not generate and end of file.
It just generates a record - an empty one, but still a record. You won't
"normally" see an end-of-file on an interactive terminal, because there
is always the possibility that the user will type more input. On a unix
system, control-D will typically generate an interactive end-of-file,
but that's not what I'd call "normal".

> That means just proceed as normal...passing the empty string
> to the FA function should work,


I haven't seen the FA function (if it was in a previous post, I didn't
go back and check), but your terminilogy here makes me suspicious. There
is no such thing in f77 as an "empty" string. There can be an empty
record, but not an empty string. There can be a string full of blanks,
but that isn't the same thing as "empty". Quite the opposite, it is
"full"; what it is full of happens to be blanks, but those are perfectly
valid characters.

> READ(*, END=13) str


Two problems with this line. First is the one that the erro rmessage
bitched about. You don't have a format here. A format specification is
*REQUIRED* for a formatted file. You might be intending the * to be the
format specification, but it isn't. The * here is the unit "number" (*
being a special case to denote the standard input unit). You might have
intended

read(*,*,end=13) string

but that has another problem, namely that list-directed formatting (the
*) is a poor choice here. It will keep "searching" until it finds a line
with some input on it, which is not at all the behavior you want. You
probably want something more like

read(*,'(a)') string

where I have also taken off the end=, which is pretty useless here
unless you really did want to detect control-D.

--
Richard Maine | Good judgment comes from experience;
email: my first.last at org.domain| experience comes from bad judgment.
org: nasa, domain: gov | -- Mark Twain
artheartscience

2006-01-31, 7:02 pm

Wow, thanks for all the info! Everything is working great now.

Richard E Maine wrote:
> artheartscience <steve@stevedurkin.net> wrote:
>
>
> First, no. It does not mean anything close to that. The end= detects an
> end-of file. Pressing the enter key does not generate and end of file.
> It just generates a record - an empty one, but still a record. You won't
> "normally" see an end-of-file on an interactive terminal, because there
> is always the possibility that the user will type more input. On a unix
> system, control-D will typically generate an interactive end-of-file,
> but that's not what I'd call "normal".
>
>
> I haven't seen the FA function (if it was in a previous post, I didn't
> go back and check), but your terminilogy here makes me suspicious. There
> is no such thing in f77 as an "empty" string. There can be an empty
> record, but not an empty string. There can be a string full of blanks,
> but that isn't the same thing as "empty". Quite the opposite, it is
> "full"; what it is full of happens to be blanks, but those are perfectly
> valid characters.
>
>
> Two problems with this line. First is the one that the erro rmessage
> bitched about. You don't have a format here. A format specification is
> *REQUIRED* for a formatted file. You might be intending the * to be the
> format specification, but it isn't. The * here is the unit "number" (*
> being a special case to denote the standard input unit). You might have
> intended
>
> read(*,*,end=13) string
>
> but that has another problem, namely that list-directed formatting (the
> *) is a poor choice here. It will keep "searching" until it finds a line
> with some input on it, which is not at all the behavior you want. You
> probably want something more like
>
> read(*,'(a)') string
>
> where I have also taken off the end=, which is pretty useless here
> unless you really did want to detect control-D.
>
> --
> Richard Maine | Good judgment comes from experience;
> email: my first.last at org.domain| experience comes from bad judgment.
> org: nasa, domain: gov | -- Mark Twain


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com