I was trying to create a bunch of code to help kmac with his topic "Reading tabled data", but when testing, BMax does something weird and I cannot find the problem.
This is the file c:\Test.txt
The code opens the file, creates a new Data-object if EOF hasn't been reached yet, reads an entire line, copies the first 10 characters into the Name$-field of this new object, removes spaces at the end of the name and prints all names.
The problem lies in the While..Wend loop following the line "' Remove spaces at the end of the name".
It does it's job perfectly for all names, except the "Geert" name (that's my name actually).
If you comment out the commented For..Next loop, it print all characters of each name (actually the ASCII code for each char).
You'll notice that the "Geert" name has these character codes: 71,101,101,114,116,32,32,32,32,32 (ending spaces included).
The code removes the last char (the "t"-char, ASCII code 116) too, but for the "Gert" name, it doesn't do it, there it works perfectly as only the ending spaces are removed and the "t" remains in the name.
I'm trying different approaches, as looping through the entire string backwards with a For..Next loop, storing each char in a different variable and checking that against Chr(32) (= ASCII code for a space), but all results where the same.
Can somebody explain this to me?
Global DataArray:Data[] Type Data Field Name$ Field Age% Field Height% End Type Function ReadTabledData(DFH%) Local LineFromFile$ = ReadLine$(DFH%) ' Read a line from the given filehandle Local a:Data = New Data ' Create a new "Data"-object a.Name$ = LineFromFile$[..10] ' Copy the first 10 characters from the line into field Name$ ' For i = 0 Until a.Name$.length ' Print a.Name$[i] ' Next ' Print ' Remove spaces at the end of the name While Chr(a.Name$[a.Name$.length - 1]) = Chr(32) a.Name$ = a.Name$[..(a.Name$.length - 2)] Wend ' Resize the array to hold one more object DataArray = DataArray[..(DataArray.length + 1)] ' Add the new Data-object to the array DataArray[DataArray.length - 1] = a End Function ' Open a file for reading (DFH = DataFileHandle) Local DFH% = ReadFile("c:\Test.txt") ' Keep reading until EndOfFile While Not Eof(DFH%) ReadTabledData(DFH%) Wend ' Close the file CloseFile(DFH%) ' Print all names For i = 0 Until DataArray.length Print Chr(34) + DataArray[i].Name$ + Chr(34) Next End
This is the file c:\Test.txt
Johnny 20 185 Piet 27 170 Geert 26 182 Gert 28 120 Cornelis 50 150
The code opens the file, creates a new Data-object if EOF hasn't been reached yet, reads an entire line, copies the first 10 characters into the Name$-field of this new object, removes spaces at the end of the name and prints all names.
The problem lies in the While..Wend loop following the line "' Remove spaces at the end of the name".
It does it's job perfectly for all names, except the "Geert" name (that's my name actually).
If you comment out the commented For..Next loop, it print all characters of each name (actually the ASCII code for each char).
You'll notice that the "Geert" name has these character codes: 71,101,101,114,116,32,32,32,32,32 (ending spaces included).
The code removes the last char (the "t"-char, ASCII code 116) too, but for the "Gert" name, it doesn't do it, there it works perfectly as only the ending spaces are removed and the "t" remains in the name.
I'm trying different approaches, as looping through the entire string backwards with a For..Next loop, storing each char in a different variable and checking that against Chr(32) (= ASCII code for a space), but all results where the same.
Can somebody explain this to me?