Go Back   EQEmulator Home > EQEmulator Forums > Archives > Archive::Misc > Archive::Off Topic

Archive::Off Topic Archive area for Off Topic's posts that were moved here after an inactivity period of 90 days.

Reply
 
Thread Tools Display Modes
  #1  
Old 04-24-2004, 09:36 PM
nemious
Fire Beetle
 
Join Date: Mar 2003
Posts: 10
Default CV programing

this may be long but i was looking over some notes i got from the net dont remember what web page but here they are
The Ins and Outs of Files

Discussion 1: The Basics

In a former life I was a schoolteacher. I have had a few students to whom every Monday was like the first day of school. Over the weekend, they had forgotten everything they had learned the whole year.

Computer programs are like that. Programs require data to operate, but shut a program down, and it develops Alzheimer's at warp speed. And unless you've had the foresight to save the data somewhere, you're in for a bad day.

This discussion is about saving and retrieving data. Specifically, it is about creating and accessing data files with Visual Basic code.



Getting Started

Open a standard VB project.
If it is not already visible, show the Toolbox (View/Toolbox).
Accept the Form as it is.
Add 2 command buttons and 2 list boxes by double clicking on their icons in the Toolbox. Some will cover others, but that's OK.
Now create a new folder named C:\VBTalk. Add a subfolder named FileAccess and under FileAccess create a subfolder named Discussion1. Save your VB project there.


Generating sample data

The code that follows positions and sizes the command buttons and list boxes on the form and generates some sample data that we will save to a file and retrieve. Don't concern yourself with trying to analyze this code since it doesn't relate to file handling.

The purpose of the first list box (List1) is to hold the data until we are ready to use it. We could have used an array, but a list box lets us view the data.

Switch to the Code window (View/Code)
Copy the code that follows and paste it into the Code window.

Private Sub Form_Load()
Dim i As Integer, j As Integer, NumItems as integer
Dim temp As String
NumItems=24

'Positions and sizes controls
With Command1
.Height = 495
.Left = 0
.Top = 120
.Width = 1215
End With
With Command2
.Height = 495
.Left = 0
.Top = 650
.Width = 1215
.Caption = "Load Data"
End With
With List1
.Height = 2790
.Left = 1560
.Top = 120
.Width = 2895
End With
With List2
.Height = 2790
.Left = 3100
.Top = 120
.Width = 1500
End With

'Generates 6-letter words composed of random letters
For i = 0 To NumItems
For j = 1 To 6
temp = temp & Chr$(Int((26 * Rnd) + 65))
Next
'Add words to list box>
List1.AddItem temp
temp = ""
Next
End Sub

At this point, save the project again and run the program.

If all went well, you should see the list box filled with an array of nonsense words. If not, review the steps in the instructions.



Saving data to a text file

Let's assume that the sample data we just generated is important stuff and that we need it to survive beyond our closing the program. We need to save it to a file on the hard disk.

The first step is to create and "Open" the file. We can do this in one step with the following code:

Open File Name for Output as File Number

File Name is the name you assign to the file and must also include its path. Since we are going to put this file in the same folder as the VB project we are working on, we can use App.Path for the path portion. But since App.Path doesn't include the "\" separator, we will need to insert it in front of our file's name.

File Number is an integer which VB needs to identify open files. We can have VB assign this number by using the FreeFile function:

Dim Fn as integer
Fn = FreeFile.

Or we can assign the number ourself. This is OK if we are sure that the number we assign is not being used by another open file. We can't be sure, of course, if our program is going to be run by someone else, so it's safest to let VB assign the file number.

Output tells VB we are sending stuff out to the file so it can get its act together. That act includes creating the file if it doesn't already exist. It also includes erasing everything in the file if it does exist. So we must bear that in mind when we use this method of Open.

Let's name our file "SampleWords.txt" Here is the code to open it.

Dim Fn as Integer
Fn = FreeFile
Open App.Path & "\SampleWords.txt" for Output as Fn

Now that the file is opened, we need to copy our data to it. The code for this is:

Print # Fn, data item.

And when we have finished copying the data items, we need to close the file:

Close Fn.

Since we are using a List Box control, List1, to hold the data we want to save, we have to get it out of there and feed it to the file an item at a time. Since we know how many items there are, List1.ListCount tells us, we can use a For/Next loop to do the job. And we will start the numbering at zero, since that's the way VB does it for the list box items.

Here is the complete code for saving the contents of the List Box to the file.


Private Sub Command1_Click()
Dim i as integer, Fn as integer
Fn = FreeFile
Open App.Path & "\SampleWords.txt" for Output as Fn
For i = 0 to List1.ListCount-1
Print #1, List1.List(i)
Next i
Close Fn
End Sub

Copy and paste it in the Code window of your VB project. Now run the program.



Did it happen?

Go to the folder, C:\VB Talk\FileAccess\Discussion1, where you saved this VB project. Find the file named "SampleWords.txt" and open it. You should see the data contained in List1 duplicated here.



Getting data from a text file

To get the data from the file into the program, we have to Open the file for Input rather than Output. Otherwise the code is the same:

Open App.Path & "\SampleWords" for Input as Fn.
We will copy the data an item at a time just as we stored it, but in this operation we need a string variable to hold the data temporarily. Call the variable Temp. And we use Input rather than Print:

Input #Fn, Temp.
To cycle through the items in the file, we will use a Do loop rather than a For/Next loop. The advantage is that we do not have to know how many items are in the file. We just stop the loop when the end of the file is encountered using EOF(Fn).(The EOF function will be discussed in the next section.)

Finally, we will put the data in the second list box, List2.

The code is:


Private Sub Command2_Click()
Dim i as Integer, Fn as Integer
Dim Temp as string
Fn = FreeFile
Open App.Path & "\SampleWords.txt" for Input as Fn
Do until EOF(Fn)
Input #Fn, Temp
List2.AddItem Temp
Loop
Close Fn
End Sub

Copy the code above, paste it into the code window of your project and run it.

If all went well, the items in List2 should match the items in List1, demonstrating that we have successfully saved and retrieved the data from List1.



Adding data to a text file

If you need to add data to an existing file while preserving the data already there, then Open the file for Append:

Open App.Path & "\SampleWords.txt" for Append as Fn
The file opens with VB's file pointer conveniently placed at the end of the existing data. You can add additional data using Print #, data item.

So far we have discussed the most basic features of File input/output functions. The next section will explore these functions a little deeper
The Ins and Outs of Files

Discussion 2: Using Line Input and EOF to Get Data

Line Input

When VB stores data in a file, it must mark the boundaries of each item, otherwise it wouldn't be able to retrieve it. When you use the Print # statement, VB puts a line feed symbol and a carriage return symbol after each item in the file. When you retrieve the data using the Input # statement, VB looks for the line feed and carriage return and hands you what's in front of it. Then it skips over the line feed and carriage return and puts its finger down to mark its place (well, it's formally called the file pointer) and gets ready to read again when called upon.

(VB can be forced to read from any place in a file by manipulating the file pointer, but we'll save that for later discussion.)

At present, it is important to note that there are symbols other than the line feed/carriage return pair that cause VB to stop and deliver what it has read. A comma can do it, for instance. Try this:

Open a standard VB project.
Create a new subfolder under C:\VBTalk\FileAccess\ named Discussion2 and save your VB project there..
In the form's properties window (View/Properties window), set AutoRedraw to True
Display the Code page by choosing View/Code from the menu.
Copy the following code, paste it into your project and run it:

Private Sub Form_Load()
Dim Fn As Integer
Dim Temp As String
Fn = FreeFile
Open App.Path & "\TestData.txt" For Output As Fn
Print #1, "Frankfort, KY"
Close Fn
Open App.Path & "\TestData.txt" For Input As Fn
Input #1, Temp
Close Fn
Form1.Print Temp
End Sub

What we are doing here is opening a file for Output and storing the item "Frankfort, KY" in it. Then we close the file and open it for Input and read the item into a variable named Temp. We print on the form the contents of Temp, and discover that item has been truncated at the comma and contains only Frankfort.

Be of good cheer. All is not lost. We can force VB to ignore delimiters such as our comma by using Line Input instead of just Input.

Try it out. Go to your project code window. Change the line

Input #1, Temp

to

Line Input #1, Temp

and rerun the program.

Line Input returns the whole Item. Line Input comes in quite handy when you are reading punctuated text from a file.

End of file (EOF)

Above, we mentioned the end-of-file marker. It is important to know when we have read all the data in the file because if we ask VB to read beyond the last item, it rewards us with an program-breaking error.

Replace the code in your VB project with the following:


Private Sub Form_Load()
Dim Fn As Integer, i As Integer
Dim Temp As String
Fn = FreeFile
Open App.Path & "\TestData.txt" For Output As Fn
Print #Fn, "Jack"
Print #Fn, "Jill"
Close Fn
Open App.Path & "\TestData.txt" For Input As Fn
For i = 1 To 3
Input #Fn, Temp
Form1.Print Temp
Next i
Close Fn
End Sub


Now run the project. Oops
Reply With Quote
  #2  
Old 04-25-2004, 01:41 AM
NarutoLegacy
Discordant
 
Join Date: Feb 2004
Posts: 400
Default

You can't expect me to read that whole thing.... O_O
__________________
My Newbie Guides
Server Classifications
Zone Points
Change Maxlevel

Kaio Kuzumaki of the East, Owner of Tides of War [Custom-Legit] PvP Server
Orome, Lord of the Woods, Middle-Earth Roleplay PvP Server
Kaio Kuzumaki of the East, Owner of Kaio's All-GM Test Server
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

   

All times are GMT -4. The time now is 07:26 PM.


 

Everquest is a registered trademark of Daybreak Game Company LLC.
EQEmulator is not associated or affiliated in any way with Daybreak Game Company LLC.
Except where otherwise noted, this site is licensed under a Creative Commons License.
       
Powered by vBulletin®, Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Template by Bluepearl Design and vBulletin Templates - Ver3.3