Re: removing blanks from a file
- From: nospam@xxxxxxxxxxxxx (Richard Maine)
- Date: Thu, 29 Jun 2006 19:54:33 -0700
Others gave some alternate suggestions. I'll look at the code. It isn't
handy to run it here at the moment, but my comments from inspection are
interlaced with the code below.
Luka Djigas <ldigas@@gmail.com> wrote:
CHARACTER(1) :: CH
INTEGER(4) :: IOEND=0
Why do you specify a kind value of 4? It achieves nothing at all useful
here. All it does is make the code system-dependent for no good reason.
The value 4 is not gieranteed to be a valid kind value at all (and it
isn't on some compilers). I recommend just using default integer here
(and most places). It is simpler and portable. There are places where
system-dependent constructs make sense, but I see no sense in it here.
OPEN(1,FILE='FILE.TXT')
While it is probably ok, I generally advise avoiding unit numbers less
than 10; they are prone to system-dependent special use. Unit isn't
*usually" reserved, but it has been known to happen.
DO WHILE (IOEND/=-1)
Three separate issues here.
1. It is my observation that "do while" rarely fits well with what is
really wanted. This is a typical example. This is the wrong place in the
loop for the test. When you hit and end-of-file below, you will write
out some undefined garbage value before getting back to this test. I
suggest instead an "infinite DO" here, i.e. just plain old DO. Then put
the test right after the READ statement and EXIT the loop when you hit
the EOF. I do recommend using a loop construct name when you have an
EXIT statement so that you can make it clear what is being exited.
2. Realize that the value -1 is system-dependent. Usually I advise just
testing for negative, but since you are doing nonadvancing I/O, you will
see two negative values; one for end-of-file, and one for end-of-record.
Probably easiest to just accept the system dependence here. F2003
provides a simple, portable way to distinguish the two, but it is a
bother in f90/f95, so I'd say you are ok. Still, be aware that it is
system-dependent.
3. Speaking of end-of-record, why aren't you handling that? You are
ignoring it and writing a presumably garbage character when you hit the
end of record. You should test for end-of-record (the only negative
value allowed other than the eof one, so if it is negative and not eof,
you have an eor). When you hit and end of record, do an advancing write
(of no data - just a write of nothing) to end the output record.
READ(1, "(A1)", IOSTAT=IOEND, ADVANCE="NO")CH
IF CH=CHAR(49)
CH=CHAR(50)
Two questions on these two lines. Possibly related.
1. Why are you using the cryptic char(49) and char(50) instead of just
literal character constants? There are reasons to use char() forms -
notably when you are working with non-printable charactersthat you might
not be able to put literally in source code, but that isn't the case
here.
2. And why are you changing all occurances of the character "1" to the
character "2"? That doesn't seem to have anything to do with what you
said you were trying to do. Is this perhaps a side effect of using the
cryptic form? That is, did you thing char(49) was something other than
"1" and char(50) was something other than "2"? Or is this perhaps some
character set other than ASCII?
To match what you said you were trying to do, you should instead test
for " " (which is char(32) if you really want to write it that way, but
I advise against it). Then skip over the write if you found a blank. The
easiest way to skip the write here would be with a CYCLE statement,
which cycles back to th etop of the loop.
WRITE(1, "(A1)", ADVANCE="NO")CH
END DO
CLOSE(1)
END
That's all that I noticed.
--
Richard Maine | Good judgement comes from experience;
email: last name at domain . net | experience comes from bad judgement.
domain: summertriangle | -- Mark Twain
.
- References:
- removing blanks from a file
- From: Luka Djigas
- removing blanks from a file
- Prev by Date: linking g95-built library from VC++
- Next by Date: Re: Efficient way to pass different shaped arrays?
- Previous by thread: Re: removing blanks from a file
- Next by thread: Re: removing blanks from a file
- Index(es):
Relevant Pages
|