Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!ames!skipper!elxsi!maine From: maine@elxsi.dfrf.nasa.gov (Richard Maine) Newsgroups: comp.lang.fortran Subject: Re: length of a character string Message-ID: Date: 19 Sep 89 20:29:31 GMT References: <18585@ut-emx.UUCP> Sender: news@skipper.dfrf.nasa.gov Organization: NASA Dryden, Edwards, Cal. Lines: 53 In-reply-to: reeder@ut-emx.UUCP's message of 19 Sep 89 16:14:44 GMT In article <18585@ut-emx.UUCP> reeder@ut-emx.UUCP (William P. Reeder) writes: > Anyway, I need to check for a null string in variable "ncarg". I thought > about using the intrinsic function LEN, but that just told me the > size of the character variable (80), not the length of the string stored > there (which may be less than 80). Is this the proper behavior for LEN? > I can see where you would want to know how much storage was available > in a character variable, but I want to know the length of a string stored > in that variable much more frequently. I didn't see any standard > intrinsic function defined which might give me this information. Yes, its behavior is correct. The situation where I know that LEN is of most use is in a subroutine with a character*(*) argument. Of course it can also be useful to avoid hard-coding constants multiple places in the program so that later changes are easier. There is no way (within the standard) to do what you are trying to do. FORTRAN 77 does not allow null strings in any context, so you are not going to be able to come up with a standard way of making or detecting them. In fact, FORTRAN 77 has no concept of variable-length strings at all (niether does 8x, if I recall correctly). That's why LEN doesn't do what you think, because FORTRAN 77 does not support the concept you are thinking of. The character*(*) arguments are not variable length, though many people sloppily use such terminology for them (the standard doesn't). > The best idea I have come up with so far for creating a "null" string > is this: > character*80 null > write ( null, * ) That won't work either, for 2 reasons. 1) There ain't no such animal as a null string. This will just fill the string with blanks, which is quite a different thing than a null string. 2) List-directed i/o to internal files is illegal anyway. Some compilers support it, but not all. (8x does allow it). > Does anyone have any better ideas? If you want to check for a blank string, that's easy, but I assume you are making a distinction here. If you really want to check for null strings, its going to have to be outside of the context of standard FORTRAN. Perhaps there is a system call you can use to get the required info instead of trying to get the string into a FORTRAN string and then deduce things from that. -- Richard Maine maine@elxsi.dfrf.nasa.gov [130.134.1.1]