Re: Is this a bug, or am I being stupid?

new topic     » goto parent     » topic index » view thread      » older message » newer message

Jacques Guy writes:
> object fileName,infile
> printf(1,"%s",{"File name, please: "})
> fileName=gets(0)
> infile=open(fileName,"r")
> reset(infile)
> if infile=-1
> then printf(1,"\n%s %s\n",{fileName,"does not exist."})
> else printf(1,"\n%s %s\n",{fileName,"found."})
> end if

> Well, it doesn't work. It always reports a non-existent
> file, because gets(0), I found, returns a string with a
> linefeed appended (hex 10). Of course, I could strip
> that linefeed off, but there must be a better way.

> What have missed?

You haven't missed anything. Just do:

     fileName = fileName[1..length(fileName)-1]

to remove the linefeed. gets() will never return
a length-0 string so this will always work. It will return
the atom -1 at end of file, which can only happen if the user
types control-Z.

I originally wanted to avoid this awkwardness by having
gets() trim off the linefeed character automatically,
but I found that the last line in a file might not have a linefeed.
It might end abruptly with end-of-file. So in order that you be able
to tell if there's a linefeed or not, gets() will always include
the linefeed if there is one (99.9% of the time there *is*).

Regards,
   Rob Craig
   Rapid Deployment Software

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu