Where have I gone wrong

General FreeBASIC programming questions.
Post Reply
Gablea
Posts: 1104
Joined: Apr 06, 2010 0:05
Location: Northampton, United Kingdom
Contact:

Where have I gone wrong

Post by Gablea »

hi all,

I need some guidance

I am trying to Access a random data file that is being generated by VB.net application but I am getting a few error messages at the moment i can not even try to read the file.


this is my code so far

Code: Select all

Type CashierDB Field = 1
		    	           Dim CashierNumber(3) As UByte
					      Dim CashierPassword(3) As UByte
    					     Dim CashierName(254) As UByte
    			    Dim CashierNameReceipit(39) As UByte
			             Dim ChangePassword(0) As UByte
    				  		 Dim RestrictRefund(0) As UByte
    				  Dim RestrictRefundValue(9) As UByte
    			  Dim UserCanOverrideLockout(0) As UByte
    				    Dim useraccesslevel(254) As UByte

    'these below this line are future update to the NPoS & KeyPoS (AND MAYBE KPoS)
    'DIM DiscountLockout(0) As UByte
    'DIM Discountvalue(9) As UByte
    'DIM OverrideDiscountLock(0) As UByte
	End Type

Code: Select all

Public Sub FindCashier (ByVal CashierNumber As String, ByVal CashierPassword As String)
	Dim recordLength			As Integer = 0
	Dim recordNumber			As Integer = 0
	Dim lastRecord				As Integer = 0
	Dim CashierFound 			As Integer = 0
	
	
	recordLength = len(CashierRecord)
	Cls
	
	Print "Path to Database : "; PathToCashierDatabase

	Print "Cashier Number To Find :"; CashierNumber
	Print "Cashier Password : "; CashierPassword
	Print "Database Record Length :"; recordLength
	
	Open PathToCashierDatabase For Random Access Read Write As #CashierDatabaseFileNumber len = recordLength
	
	lastRecord = lof(CashierDatabaseFileNumber) \ recordLength
		Print "lof(CashierDatabaseFileNumber)="; lof(CashierDatabaseFileNumber)
		Print "lastRecord"; lastRecord
		
	For recordNumber = 1 to lastRecord
   	get #CashierDatabaseFileNumber, recordNumber, CashierRecord
  	  	
  		Select Case CashierRecord.CashierNumber
  			Case = cashierNumber Then
  				If CashierRecord.CashierPassword = CashierPassword Then
  					Select Case Trim(CashierRecord.ChangePassword) ' Check to see if Password Needs to be changed
  						Case "0"	' Password Does not need to be changed
									    KeyPadInput = ""
			    	  			  CashierNamePrint = Trim(CashierRecord.CashierNameReceipit)
				        		     CashierAccess = Trim(CashierRecord.useraccesslevel) 	'Set the system to use the signed on cashier 
							CashierOverrideAccess = 0
							   PriceOverridePrice = 0               			'Clears the Entered Price 
					       				 TotalDue = 0 								'Resets sale value
					    				   ItemsSold = 0 								'Resets item count
					     			  TotalTendered = 0								'Resert the total tendered value
					 				   RecipitClear = 1								'Set the recipit so a new one can be produced for sale
					 		    		    SaleMode = "Sale"							'Sets the salemode back to sale
								 'RecallInProgress = "No"							'Resets the Recall trigger so the system will run in normal mode
					  			  ShowTaskBarItems = 1
					 				SubTotalPressed = 0
				         			DisplayLine1 = ""
										DisplayLine2 = ""		
							Salescreen
						
  						Case "1"
						 	KeyPadInput = ""
			  				'PasswordChange1
  					End Select
  				Else
  					updateCashierScreen(CashierDisplayComSettings, "USER PASSWORD DOES", 0, "NOT MATCH. PRESS CLEAR" & KeyPadInPutPassword, 1, 0)
						Do : Dim KeyPress As Long = GetKeyNB
							Select Case KeyPress
								Case Key_Clear
									KeyPadInput = ""
									CloseAllFiles
									RequestCashierPassword(CashierNumber)
								Exit Sub
							End Select
  				End If
			Case <> cashierNumber
				updateCashierScreen(CashierDisplayComSettings, "USER NOT FOUND", 0, "PRESS CLEAR" & KeyPadInPutPassword, 1, 0)
					Do : Dim KeyPress As Long = GetKeyNB
						Select Case KeyPress
							Case Key_Clear
								KeyPadInput = ""
								RequestCashierID
								Exit Sub
						End Select
			End If
  		End Select
	Next	
	
	Close #CashierDatabaseFileNumber

 End Sub
This is the error mesasges
  • C:\FreeBASIC\FreeBASIC - Projects\KPoS\database.bi(104) error 72: Array access, index expected in 'Select Case CashierRecord.CashierNumber'
    C:\FreeBASIC\FreeBASIC - Projects\KPoS\database.bi(105) error 9: Expected expression, found '=' in 'Case = cashierNumber Then'
    C:\FreeBASIC\FreeBASIC - Projects\KPoS\database.bi(105) error 3: Expected End-of-Line, found '=' in 'Case = cashierNumber Then'
    C:\FreeBASIC\FreeBASIC - Projects\KPoS\database.bi(106) error 72: Array access, index expected, found '=' in 'If CashierRecord.CashierPassword = CashierPassword Then'
    C:\FreeBASIC\FreeBASIC - Projects\KPoS\database.bi(107) error 72: Array access, index expected, found ')' in 'Select Case Trim(CashierRecord.ChangePassword) ' Check to see if Password Needs to be changed'
    C:\FreeBASIC\FreeBASIC - Projects\KPoS\database.bi(108) error 24: Invalid data types, before ''' in 'Case "0" ' Password Does not need to be changed'
    C:\FreeBASIC\FreeBASIC - Projects\KPoS\database.bi(110) error 72: Array access, index expected, found ')' in 'CashierNamePrint = Trim(CashierRecord.CashierNameReceipit)'
    C:\FreeBASIC\FreeBASIC - Projects\KPoS\database.bi(111) error 72: Array access, index expected, found ')' in 'CashierAccess = Trim(CashierRecord.useraccesslevel) 'Set the system to use the signed on cashier'
    C:\FreeBASIC\FreeBASIC - Projects\KPoS\database.bi(126) error 24: Invalid data types in 'Case "1"'
    C:\FreeBASIC\FreeBASIC - Projects\KPoS\database.bi(140) error 29: Expected 'LOOP', found 'End' in 'End If'
    C:\FreeBASIC\FreeBASIC - Projects\KPoS\database.bi(140) error 132: Too many errors, exiting
Am I accessing the CashierRecord.CashierNumber in the wrong way? or am I not doing this the logical way ANY hints or points are most welcomed as I have been at this for 4 hours
badidea
Posts: 2586
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: Where have I gone wrong

Post by badidea »

You use arrays of ubytes, I think you what to use string * size. But be aware of the additional NULL character needed at the end. I don't know the format that VB outputs. You have to know the exact file format that is used.
See: https://freebasic.net/wiki/wikka.php?wakka=KeyPgString
Try to make a small test program first, instead of something that needs a lot of shared variables (which are missing above).
Gablea
Posts: 1104
Joined: Apr 06, 2010 0:05
Location: Northampton, United Kingdom
Contact:

Re: Where have I gone wrong

Post by Gablea »

this is how I am outputting the data from VB.net

Code: Select all

Public Structure CashierStructure
    <VBFixedString(4)> Public CashierNumber As String
    <VBFixedString(4)> Public CashierPassword As String
    <VBFixedString(255)> Public CashierName As String
    <VBFixedString(40)> Public CashierNameReceipit As String
    <VBFixedString(1)> Public ChangePassword As String
    <VBFixedString(1)> Public RestrictRefund As String
    <VBFixedString(10)> Public RestrictRefundValue As String
    <VBFixedString(1)> Public UserCanOverrideLockout As String
    <VBFixedString(255)> Public useraccesslevel As String

    'these below this line are future update to the NPoS & KeyPoS (AND MAYBE KPoS)
    '<VBFixedString(1)> Public DiscountLockout As String
    '<VBFixedString(10)> Public Discountvalue As String
    '<VBFixedString(1)> Public OverrideDiscountLock As String
End Structure

Code: Select all

    Public Sub ExportCashierFile_FixedLength()
        Dim CashierData As New CashierStructure
        Dim FileData As String = vbNullString
        Dim FileName As String = My.Settings.DOSDB_Location & "\users.dat"

        Dim FileNumber As Integer = FreeFile()

        If File.Exists(FileName) = True Then Kill(FileName)

        DisplayMessage(" Exporting Cashier Table", "Export")

        SQLCommand = vbNullString
        SQLCommand += "Select "
        SQLCommand += "userid, "
        SQLCommand += "userpassword, "
        SQLCommand += "username, "
        SQLCommand += "usernameposrecipit, "
        SQLCommand += "changepassword, "
        SQLCommand += "restricrefundvalue, "
        SQLCommand += "refundvalue, "
        SQLCommand += "usercanoverridelockout, "
        SQLCommand += "useraccesslevel "
        SQLCommand += "from usertable "
        SQLCommand += "Where usertype='P';"



        With Me
            ProgressBar1.Value = 0
            .ProgressBar1.Maximum = RecordCount("select count(userid) from usertable where usertype='P'")
        End With

        Try
            ConnecttoDatabase()

            Dim dr As MySqlDataReader

            MySQLCmd = New MySqlCommand(SQLCommand, dbCon)

            'Connect to the Database and find the SQLCommand
            If dbCon.State = ConnectionState.Open Then
                dbCon.Close()
            End If

            dbCon.Open()

            dr = MySQLCmd.ExecuteReader

            If dr.HasRows Then
                FileOpen(FileNumber, FileName, OpenMode.Random, OpenAccess.Write, OpenShare.LockWrite, Len(CashierData))

                While dr.Read
                    FileData = vbNullString
                    Me.ProgressBar1.Value += 1
                    DisplayPercentage(ProgressBar1.Value, ProgressBar1.Maximum)


                    With CashierData
                        .CashierNumber = dr.Item("userid")
                        .CashierPassword = dr.Item("userpassword")
                        .CashierName = Trim(dr.Item("username"))
                        .CashierNameReceipit = Trim(dr.Item("usernameposrecipit"))
                        .ChangePassword = dr.Item("changepassword")
                        .RestrictRefund = dr.Item("restricrefundvalue")
                        .RestrictRefundValue = dr.Item("refundvalue")
                        .UserCanOverrideLockout = dr.Item("usercanoverridelockout")
                        .useraccesslevel = dr.Item("useraccesslevel")
                    End With


                    DisplayPercentage(ProgressBar1.Value, ProgressBar1.Maximum)
                    Application.DoEvents()

                    'save record
                    FilePut(FileNumber, CashierData, ProgressBar1.Value)
                End While

                'close the file
                FileClose(FileNumber)

                With dbCon
                    .Dispose()
                    .Close()
                End With
            End If

        Catch ex As Exception
            MsgBox(CashierData.CashierNumber & " " & CashierData.CashierName & " " & ex.Message)
            MySQLDR.Close()
            If dbCon.State = ConnectionState.Open Then
                dbCon.Close()
            End If
        Finally
            dbCon.Close()
            DisplayMessage(" DONE", "Export_Done")
        End Try
    End Sub
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Where have I gone wrong

Post by MrSwiss »

Where have I gone wrong -- Some time ago, I'd say, because:
you should have taken some time, to educate yourself, before tackling such a
complex endeavour, which, without some basic know-how, is "unlikely successful".

Looking at the VB.net code and, by comparison your FB code, I get the impression,
that the VB.net code wasn't coded by you ... (sorry, but your code is just a mess).

That, and no structure or concept of coding, nor any visible project structure, will
make this whole POS thingy, next to impossible to achieve ...

I've tried to help in the past, but to be honest, I see it as a: "lost case/battle".
(I'm definitely out of it, as of now! Reason: my time is better spent, otherwise.)

A last bit of advice: attend some courses, teaching coding concepts and/or at least
some project management skills, without any of those, you'll not ever "get there".
(Within a reasonable time frame, that is.)
Zippy
Posts: 1295
Joined: Feb 10, 2006 18:05

Re: Where have I gone wrong

Post by Zippy »

MrSwiss wrote:Where have I gone wrong -- Some time ago, I'd say, because:
you should have taken some time, to educate yourself, before tackling such a
complex endeavour, which, without some basic know-how, is "unlikely successful".

Looking at the VB.net code and, by comparison your FB code, I get the impression,
that the VB.net code wasn't coded by you ... (sorry, but your code is just a mess).

That, and no structure or concept of coding, nor any visible project structure, will
make this whole POS thingy, next to impossible to achieve ...

I've tried to help in the past, but to be honest, I see it as a: "lost case/battle".
(I'm definitely out of it, as of now! Reason: my time is better spent, otherwise.)

A last bit of advice: attend some courses, teaching coding concepts and/or at least
some project management skills, without any of those, you'll not ever "get there".
(Within a reasonable time frame, that is.)
Some day, Master Swiss, when you are old enough to vote and procreate, please don't.

You lack grace. The unfortunate thing [for everyone else] is that you may never understand this.
.
grindstone
Posts: 862
Joined: May 05, 2015 5:35
Location: Germany

Re: Where have I gone wrong

Post by grindstone »

@Gablea:
My recommendation: Read the whole record into one string, and then parse the string to the several type members. And, of course, like badidea mentioned, use fixed length strings instead of Ubyte arrays.

Edit:
Alternatively, you could use properties (instead of the type members) to cut the requested parts out from the record string. This way you can circumvent the NULL terminator issue. A simple example what I'm talking about:

Code: Select all

Type record
	Dim onestring As String*10
		
	Declare Property member1 As String
	Declare Property member2 As String
	Declare Property member3 As String
End Type

Property record.member1 As String
	Return Mid(this.onestring, 1, 3)
End Property

Property record.member2 As String
	Return Mid(this.onestring, 5, 2)
End Property

Property record.member3 As String
	Return Mid(this.onestring, 6, 5)
End Property

Dim As record r
r.onestring = "1234567890"

? r.onestring;"*"
?
? r.member1;"*"
? r.member2;"*"
? r.member3;"*"

Sleep
Gablea
Posts: 1104
Joined: Apr 06, 2010 0:05
Location: Northampton, United Kingdom
Contact:

Re: Where have I gone wrong

Post by Gablea »

@MrSwiss
I have you know I did indeed code the VB.net program and I have customers running my Visual Basic version of my PoS systems since windows 3.11 was active

My strength is in Visual Basic but that will not run on Linux and I did a long time ago use qbaisc to do simple tasks but of course FreeBASIC is a lot more powerful so it has a longer learning curve.

This is a forum to exchange ideas and advice not some where for people to put others down. So in the future if you do not have anything positive or helpful to say then please keep your comments to yourself.
adele
Posts: 47
Joined: Jun 13, 2015 19:33

Re: Where have I gone wrong

Post by adele »

MrSwiss wrote:Where have I gone wrong -- Some time ago, I'd say, because:
you should have taken some time, to educate yourself, [..]
(sorry, but your code is just a mess).
[..]
That, and no structure or concept of coding, [..]
nor any visible project structure, will
[..]my time is better spent, otherwise.)
A last bit of advice:..
Si tacuisses, philosophus mansisses.
MrSwiss wrote: that is.
"my time is better spent, otherwise": Maybe in not commenting?
Thatś all.
Adi
dodicat
Posts: 7967
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Where have I gone wrong

Post by dodicat »

Many good programmers are too stuffy (or frightened) to ask questions IMO.
Adele's Latin quote applies to them also.
(If you had remained silent, you would have remained a philosopher).
And they remain silent regarding questions.

Gablea breaks this mould and asks questions openly and freely, thus you have a good programmer who is not afraid to seek help.
You know he is a good programmer because his questions are difficult and he has answered all his questions before in a different version of basic anyway.

He is the only member I see who is trying to create a business application in FreeBASIC. Something to run for hours on end without crashing or leaking.
(difficult)
When he completes his task it will be a markup for FreeBASIC.
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: Where have I gone wrong

Post by jj2007 »

Hi Gablea,

It would help if you posted (in code tags):
a) complete code ready for compiling (not bits and pieces here and there)
b) a short sample, e.g. 20 lines, of the file you want to read in
c) a short description or example of the expected result.

And please, don't feed the troll.
grindstone
Posts: 862
Joined: May 05, 2015 5:35
Location: Germany

Re: Where have I gone wrong

Post by grindstone »

jj2007 wrote:Hi Gablea,

It would help if you posted (in code tags):
a) complete code ready for compiling (not bits and pieces here and there)

b) a short sample, e.g. 20 lines, of the file you want to read in
c) a short description or example of the expected result.

And please, don't feed the troll.
I agree. Especially those two would be helpful.
Gablea
Posts: 1104
Joined: Apr 06, 2010 0:05
Location: Northampton, United Kingdom
Contact:

Re: Where have I gone wrong

Post by Gablea »

http://www.algpos.co.uk/freebasic/users.dat
http://www.algpos.co.uk/freebasic/products.dat

I have uploaded the acual file to my server so you can see just what my VB app outputs.

If needed i could work a way of stripping out the null chr from the data file once it has been created

I did have this working perfectly a while ago but that was with my VB6 app since moving to VB.net it never worked with the data file that was generated (that was why i make the error of moving to a CSV file format)

I had a working commercial Version of the PoS about 4yrs ago and it was about 90% complete ( things like printer status etc was still to be added but it managed to run in a shop for 40 days with out crashing (ran on MS-DOS 6.22))

Thank you to everyone who is willing to help and the code I posted on the first part is the full code (apart from the globally declared items)

so what should happen

the user would enter the cashier number 1105 and password of 1105

then the system should search the data file for 1105 and then check the password if it matches it would then carry on to the salescreen if the password is wrong it should display a error message on the screen saying "Password not right"

if for some reason the cashier number could not be found it should then show the error message "Cashier ID not found"
badidea
Posts: 2586
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: Where have I gone wrong

Post by badidea »

Hex dump of users.dat (first record, 571 bytes), so no NULL-character between record-members. Unsed characters filled with spaces (&h20 = 32).

Code: Select all

00000000 31 20 20 20 │ 31 32 33 34 │ 4D 69 6B 65 │ 20 20 20 20 │ 20 20 20 20 │ 20 20 20 20  1   1234Mike
00000018 20 20 20 20 │ 20 20 20 20 │ 20 20 20 20 │ 20 20 20 20 │ 20 20 20 20 │ 20 20 20 20
00000030 20 20 20 20 │ 20 20 20 20 │ 20 20 20 20 │ 20 20 20 20 │ 20 20 20 20 │ 20 20 20 20
00000048 20 20 20 20 │ 20 20 20 20 │ 20 20 20 20 │ 20 20 20 20 │ 20 20 20 20 │ 20 20 20 20
00000060 20 20 20 20 │ 20 20 20 20 │ 20 20 20 20 │ 20 20 20 20 │ 20 20 20 20 │ 20 20 20 20
00000078 20 20 20 20 │ 20 20 20 20 │ 20 20 20 20 │ 20 20 20 20 │ 20 20 20 20 │ 20 20 20 20
00000090 20 20 20 20 │ 20 20 20 20 │ 20 20 20 20 │ 20 20 20 20 │ 20 20 20 20 │ 20 20 20 20
000000A8 20 20 20 20 │ 20 20 20 20 │ 20 20 20 20 │ 20 20 20 20 │ 20 20 20 20 │ 20 20 20 20
000000C0 20 20 20 20 │ 20 20 20 20 │ 20 20 20 20 │ 20 20 20 20 │ 20 20 20 20 │ 20 20 20 20
000000D8 20 20 20 20 │ 20 20 20 20 │ 20 20 20 20 │ 20 20 20 20 │ 20 20 20 20 │ 20 20 20 20
000000F0 20 20 20 20 │ 20 20 20 20 │ 20 20 20 20 │ 20 20 20 20 │ 20 20 20 20 │ 20 20 20 4D                         M
00000108 69 6B 65 20 │ 20 20 20 20 │ 20 20 20 20 │ 20 20 20 20 │ 20 20 20 20 │ 20 20 20 20  ike
00000120 20 20 20 20 │ 20 20 20 20 │ 20 20 20 20 │ 20 20 20 30 │ 30 00 00 00 │ 00 00 00 00                 00.......
00000138 00 00 00 30 │ 31 31 31 30 │ 30 31 31 30 │ 32 31 31 31 │ 30 31 31 31 │ 31 31 31 31  ...011100110211101111111
00000150 31 31 31 32 │ 31 31 30 30 │ 30 30 20 20 │ 20 20 20 20 │ 20 20 20 20 │ 20 20 20 20  1112110000
00000168 20 20 20 20 │ 20 20 20 20 │ 20 20 20 20 │ 20 20 20 20 │ 20 20 20 20 │ 20 20 20 20
00000180 20 20 20 20 │ 20 20 20 20 │ 20 20 20 20 │ 20 20 20 20 │ 20 20 20 20 │ 20 20 20 20
00000198 20 20 20 20 │ 20 20 20 20 │ 20 20 20 20 │ 20 20 20 20 │ 20 20 20 20 │ 20 20 20 20
000001B0 20 20 20 20 │ 20 20 20 20 │ 20 20 20 20 │ 20 20 20 20 │ 20 20 20 20 │ 20 20 20 20
000001C8 20 20 20 20 │ 20 20 20 20 │ 20 20 20 20 │ 20 20 20 20 │ 20 20 20 20 │ 20 20 20 20
000001E0 20 20 20 20 │ 20 20 20 20 │ 20 20 20 20 │ 20 20 20 20 │ 20 20 20 20 │ 20 20 20 20
000001F8 20 20 20 20 │ 20 20 20 20 │ 20 20 20 20 │ 20 20 20 20 │ 20 20 20 20 │ 20 20 20 20
00000210 20 20 20 20 │ 20 20 20 20 │ 20 20 20 20 │ 20 20 20 20 │ 20 20 20 20 │ 20 20 20 20
00000228 20 20 20 20 │ 20 20 20 20 │ 20 20 20 20 │ 20 20 20 20 │ 20 20 20 (next record starts here)
"RestrictRefundValue" seems to be all NULL-chracters

The csv-file format was a bit more compact :-)

The code you posted in a previous topic (https://freebasic.net/forum/viewtopic.php?f=3&t=26946) used this for the variables:

Code: Select all

	Dim CashierNumber_Local							As String
	Dim CashierPassword_Local						As String
	Dim CashierName_Local							As String
	Dim CashierPrint_Local							As String
	Dim CashierChangePassword_Local				As String
	Dim CashierResctritedRefund_Local			As String
	Dim CashierResctritedRefundValue_Local		As String
	Dim CashierOverrideLockOut_local				As String
	Dim CashierAccessLevel_Local					As String	
So everything can be fixed length string I suppose.

Enough for today. I'll have a try at a file read routine tomorrow, unless others are quicker.
paul doe
Moderator
Posts: 1730
Joined: Jul 25, 2017 17:22
Location: Argentina

Re: Where have I gone wrong

Post by paul doe »

@Gablea: try this:

Code: Select all

/'
  The original structure in VB
  
  Public Structure CashierStructure
    <VBFixedString(4)> Public CashierNumber As String
    <VBFixedString(4)> Public CashierPassword As String
    <VBFixedString(255)> Public CashierName As String
    <VBFixedString(40)> Public CashierNameReceipit As String
    <VBFixedString(1)> Public ChangePassword As String
    <VBFixedString(1)> Public RestrictRefund As String
    <VBFixedString(10)> Public RestrictRefundValue As String
    <VBFixedString(1)> Public UserCanOverrideLockout As String
    <VBFixedString(255)> Public useraccesslevel As String
    
    'these below this line are future update to the NPoS & KeyPoS (AND MAYBE KPoS)
    '<VBFixedString(1)> Public DiscountLockout As String
    '<VBFixedString(10)> Public Discountvalue As String
    '<VBFixedString(1)> Public OverrideDiscountLock As String
  End Structure
'/
/'
	FreeBasic internal string representation
'/
type FBString
	charData	as ubyte ptr
	length		as integer
	size			as integer
end type

/'
  The structure in FB
'/
type Record
  cashierNumber as string
  cashierPassword as string
  cashierName as string
  cashierNameReceipt as string
  changePassword as string
  restrictRefund as string
  restrictRefundValue as string
  userCanOverrideLockout as string
  userAccessLevel as string
end type

function readField( _
  byval aHandle as long, _
  byref aField as string, _
  byval size as uinteger ) as string
  /'
    This function reads a fixed-length field from the file and
    converts it to a common FB string. See the code below to
    learn how it works
  '/  
  aField = space( size )
  get #aHandle, , *cptr( FBString ptr, @aField )->charData, len( aField )
  
  return( aField )
end function

/'
  Main code
'/
dim as long fileHandle = freeFile()
dim as Record records( any )
dim as integer recordCount
dim as integer currentRecord

/'
  Read all the records of the 'users.dat' file into an array
'/
open "users.dat" for binary access read as #fileHandle
  do while( not eof( fileHandle ) )
    recordCount += 1
    redim preserve records( 0 to recordCount - 1 )
    
    currentRecord = recordCount - 1
    
    with records( currentRecord )
      .cashierNumber = readField( fileHandle, .cashierNumber, 4 )
      .cashierPassword = readField( fileHandle, .cashierPassword, 4 )
      .cashierName = readField( fileHandle, .cashierName, 255 )
      .cashierNameReceipt = readField( fileHandle, .cashierNameReceipt, 40 )
      .changePassword = readField( fileHandle, .changePassword, 1 )
      .restrictRefund = readField( fileHandle, .restrictRefund, 1 )
      .restrictRefundValue = readField( fileHandle, .restrictRefundValue, 10 )
      .userCanOverrideLockout = readField( fileHandle, .userCanOverrideLockout, 1 )
      .userAccessLevel = readField( fileHandle, .userAccessLevel, 255 )
    end with
  loop
close( fileHandle )

/'
  Display the records
'/
for i as integer = 0 to ubound( records )
  color 7, 0
  ? "<<< Record " & i & " >>>"
  
  with records( i )
    color 0, 7 : ? .cashierNumber
    color 0, 3 : ? .cashierPassword
    color 0, 7 : ? .cashierName
    color 0, 3 : ? .cashierNameReceipt
    color 0, 7 : ? .changePassword
    color 0, 3 : ? .restrictRefund
    color 0, 7 : ? .restrictRefundValue
    color 0, 3 : ? .userCanOverrideLockout
    color 0, 7 : ? .userAccessLevel    
  end with
next

sleep()
As far as I can tell, it reads all the data from 'users.dat'. The other one is left as an exercise for the reader ;)
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: Where have I gone wrong

Post by jj2007 »

Works fine, Paul. I tried the same with products.dat, but the NAME field seems to be one byte off for the second and lollowing records:

Code: Select all

Product=        376
Product records 2024

Num  [0000]
Pass [1293]
Name [VACUUM CLEANERS]
Recp []

Num  [0000]
Pass [1301]
Name [0    FILTERS]
Recp []

Num  [0000]
Pass [1321]
Name [0    VACUUM CLEANERS]
Recp []

Num  [0000]
Pass [1331]
Name [0    VACUUM CLEANERS]
Recp []

Num  [0000]
Pass [1351]
Name [0    VACUUM CLEANERS]
Recp []

Num  [0000]
Pass [1371]
Name [0    ELECTRICAL]
Recp []
@Gablea: Could you kindly provide also the structure of products.dat? Without that info, it will remain guesswork.
Post Reply