Saturday, April 3, 2010

Bad replies

Yes we do get them once in a while and I'm as guilty as anyone at times.  A common error is to post a reply without testing it as shown here from an actual quoted reply.
[quote]
Well where is the problem whe you know what you want to do ? : )

[code]
$towrite = ClipPut($yourdata)

FileOpen($file)
FileWrite($file,$towrite & @Crlf)
FileClose($file)
[/code]

You can define the text you put into clip as variable and then write it to a file
[/quote]

Alright, here is what's wrong
According to the help file ClipPut() will return 1 for success and 0 for failure.  That means that the variable $towrite will contain the value 1, NOT the the clipboard data so that line should have been written as

ClipPut($yourdata)
$towrite = ClipGet()

The FileOpen() should have a flag set to determine the mode, although it can be open in read plus one of the write modes simultaneously, so that should have been
$hFile = FileOpen($file, 1) ;; here I'm using the append mode.
FileWrite($hFile, $towrite) ;; Better in this case would be FileWriteLine($hFile, $towrite)
FileClose($hFile)

Now why did I use $hFile?  Because FileOpen() will actually return a handle to the file so it's better to use that handle instead of the actual file name or variable that contains the file name.

It goes without saying that a simple quick test would have shown the problem.  Like I say, we all do it on occasion but really we should take a bit more care.  The people we reply to generally are new to coding and may not see what will be obvious to the rest of us.

No comments: