How do I get the "FIleGet" Function to retrieve the record text of a random access file?

  • Thread starter Thread starter Diomtus
  • Start date Start date
D

Diomtus

Guest
Greetings everyone, as a brief introductory, I’m quite the noob to Visual Studio, so please try to be gentle with any responses you might have. Overall, I have programs that I wrote back in the days of DOS oriented "Quick basic" that I am rewriting in visual studio for compatibility with the newer windows as using an old copy of windows xp in a virtual environment is becoming more and more impractical, especially for others that wish to use my programs. In the beginning, I was very overwhelmed with the structure of visual studio and how to get things to work in it at all... and though I am still smashing into tree stumps, I am overcoming these obstacles a little at a time… on to my dilemma.



Before posting this, I read the material(s) about how to post in here, and I did note that it stated not to duplicate posts from different forums … however, I asked a question in the General Windows Forms forum and I am not sure how to go about “transferring” the question to this forum, but it was indicated by “Kyle” that my question seemed to be more of a VB question than a WF… so here I am and hopefully duplicating the question here won’t cause any problems.



I seem to be having some difficulty in reading records from random access files - using the random mode with the FileOpen fuction, I am able to create files and write (using FilePut) records to them, but the FileGet function is either not reading the records, or it’s not assigning the text in the record to the variable …



Here’s a small program I put together for testing purposes followed by an explanation of what I believe should be resulted from the code.



Public Class Form1

Public Change As Integer = 0
Public Fdir As String = "F:\TestDirectory\"
Public Fnam As String = ""
<VBFixedString(128)> Public DataS As String

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Label1.Text = "CURRENT DIRECTORY IS:"
Label2.Text = Fdir ' THIS IS CURRENT DIRECTORY
Label3.Text = "SUGGESTED GAME DATA PATH IS:"
Label4.Text = Fdir + "GameData\"
Label5.Text = "Enter the desired game data path below: << maximum path length is 128 characters >>"
FileOpen(1, Fdir + "GameDataPath.dat", OpenMode.Random, , , 128)
Label6.Text = Str(LOF(1))
'<VBFixedString(128)> Public DataS As String
If LOF(1) > 0 Then FileGet(1, DataS, 1, True)
TextBox1.Text = Trim(DataS) : Fnam = TextBox1.Text
FileClose(1)
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If TextBox1.Text <> Nothing Then
If Microsoft.VisualBasic.Right(TextBox1.Text, 1) <> "\" Then TextBox1.Text = TextBox1.Text & "\"
If TextBox1.Text <> Fnam Then Change = 1
If Change = 1 Then
If MsgBox("Confirm the Desired Game Data Path as " + UCase(TextBox1.Text), MsgBoxStyle.YesNo, "Confirmation") = MsgBoxResult.No Then Exit Sub
Try : MkDir(TextBox1.Text)
Catch ex As Exception '<Directory already exists>
End Try
DataS = LSet(TextBox1.Text, 128)
FileOpen(1, Fdir + "GameDataPath.dat", OpenMode.Random, , , 128)
FilePut(1, DataS, 1, True)
FileClose(1)
End If
End If
'Since this button is actualy a continue button, at this point the code directs the program to its main processes...
'but for purposes of this test, the program is set to close (exit) upon completion/clicking
Me.Close()
End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
'this button is used to Delete the data file created above and then Close the program
Kill(Fdir + "~GameDataPath.dat")
Me.Close()
End Sub

End Class


Starting at the top, the initial variable declarations (I dont think i need to describe anything here)



Under the form1 loader

I don’t think I need to describe the labels or the textbox functions

... Upon the first time the program is run, the "FileOpen" function initially creates the data file, which means at this point the LOF is 0[zero] - for testing purposes, I used label6 to verify the file length during run time. At this point, because LOF=0, nothing is read from the data file, this is reflected in the textbox during runtime as it is set to the null string value of DataS (which displays nothing).



Under Button1 Click

the first line ensures it does nothing if nothing is typed in the Textbox.

the second line ensures the data path that is entered ends with a backslash.

third line identifies any change to the contents of the textbox.

upon confirmation of any changes, it creates the directory if it does not already exist, and then puts the directory path the user entered into the first record of the data file using the FilePut function - after which, the program is closed ...



Running the program a second time ... Under the form1 loader

the FileOpen function opens the data file, which now has a length greater than 0, so it employs the FileGet Function... which is set to read the first record of the file, problem is that the textbox (and any other variable or label i've tried) shows nothing, and (as is obvious) I dont understand why...



initially I tried reading the record into a standard variable length string, but sorting thru Microsoft's Visual Studio Document for visual basic, I read that the string value of a FileGet function needed to be read into a fixed length string - I attempted to declare a fixed length variable "DataS". but this still did not work, I tried to insert this declaration just before the fileGet function, but it wouldn't allow it... I also tried to use the "Lset" function just previous to the FIleGet to set a length to the DataS variable, but that didn't work either (I didn't actually think this would work, but I was desperate to try anything!)



I did check the data file itself, opened it in word pad and the text was in the file exactly as I entered it (in the textbox during runtime) and the end of the record was at the 128th place, when the file was opened with the program's second run, the label6 text verified the file length of 128, so the FilePut (set with all the same parameters as instructed by the microsoft documentation, and the same as the FIleGet parameters) functioned properly... Hence, I'm at wit's end ... all these things verify the record text is there and the file is open - and all the syntax of all the "file" operations are correct and working as they should, except for the FileGet ... Is there something wrong with the syntax of my code? Or am I missing some code somewhere, for example, is there additional code that needs to be added for the FileGet function to work?



Also, Kyle, from the General Windows Forms Forum provided (as a solution) a link to Docs >> .Net >> Visual Basic Code >> Developing Applications >> “Reading From Files” … … I scanned down and read the information on “reading from fixed width text files”. Though the information in this link could be helpful as one possible solution, it would be rather impractical, as the files are quite extensive and this solution indicates a “sequential” reading style, which means the entire file would have to be read, and written to each time a single record is changed, I need to be able to read and write records independently for the program(s) to function quickly and smoothly.



Other Facts

OS = Windows 10 Home, Ver1803, Build 17134.885

Using Visual Studio 2013 Pro, Ver12, Update4 - .Net Framework, Ver 4.7

Using Visual Basic – Windows Forms Application

Continue reading...
 
Back
Top