Unexpected results reading a plain text file with NotesStream in LotusScript (or when strings contain MORE than meet the eye)
This is a little LotusScript experiment with a plain text file and a LotusScript agent using NotesStream. After reading the text from the file I try to use the string to locate a document with a corresponding key in a NotesView. While everything looks fine in the debugger, something is not correct and the lookups fail. This article has some suggestions for solutions at the end but they are not state-of-the-art! Perhaps you know other ways?
First off, I have a super-simple plain text file with a single number in it;
Then I have a super-simple LotusScript agent reading this text file, and trying to locate a Notes document with the same number from a NotesView.
The documents have all their numbers stored as text, like this;
Why stored as text? Because this is how I found the (to me) abnormality in the first place And why NotesStream? Because code with the concept herein also runs on an iSeries, where it works fine …
The LotusScript-code looks like this;
The code uses a NotesView (1) and a NotesStream (2) to open the file and read out the contents with the ReadText-method (3). Finally the code tries to locate the document in the NotesView first by a hardcoded key (4), and then by the value retrieved via NotesStream (5). In a moment you will see how the first works and the second not.
I use the LotusScript debugger and step through the code to see the contents of the variables. First breakpoint is just before the first GetDocumentByKey-call above, when I use the hardcoded key “123456” (marked as 1 below);
As you see from the variable panel, the NotesDocument “doc” hasn’t got any value at all yet (2), and you see that the strLine-variable has the value “123456” – or so it seems ….
I step the code one line down, just to see that the first GetDocumentByKey actually does find the document with the key “123456”;
So we now know that the document is “findable”
I am now ready to step the code one line more, to process the second GetDocumentByKey (marked as 1 below), now with the content from the strLine-variable, containing “123456” (2);
…but wait! A closer look at the strLine in the variables pane (3) shows that strLine doesn’t have a closing apostrophe! Something is different here and the result from the NotesStream’s ReadText is obviously containing something more other than just the text.
So, when I run the last line of code, it won’t find the document …
What on earth is going on here? Obviously it it something with the characterset or NotesStream-way of handling things.
My first attempt was to strip off whatever strLine is holding at the end of the string, along these lines;
The code above “leftifies” the content of strLine by the length of the string itself. While this code seems to “repair” the strLine string variable …
… it doesn’t find the document…
In my case I had to do far too elaborate stuff like this, where I first convert the number to a double (just so I can store huge numbers ..) and then format it back to a string with the LotusScript’s Format() function;
And now the code works!! As you see in the variables pane, both strLine and strKey look exactly the same, but now the content of strKey is obviously on the correct format …
Another way of getting the code to work is to getting rid of all 0’s, CR and LFs from the string, such as;
strLine = Replace(strLine, Chr(0), "")
strLine = Replace(strLine, Chr(10), "")
strLine = Replace(strLine, Chr(13), "")
This also works and the strLine now contain a valid string without something-at-the-end!!
But, there must be another way?! Some parameter to set on the NotesStream-object?
PS! I have also tried to use different character sets when opening the file such as UTF-8 and US-ACIII. Additionally I have tried to use other line-end flags in ReadText such as EOL_CR and EOL_CRLF. Same results unfortunately.
I also found this post, but that didn’t work for me.