A Worble Vacation (Creating a DATA File without Worbles)

By Welopez

 

 

 

I thought I was going to enjoy a nice vacation, sans Worbles, as my usual crowd had elected to take off for South America during the heat of summer here in Southern Nevada.  They packed their skis and ski-bunny suits, hopped aboard a passing buzzard bound for McCarran International in Las Vegas, and off they went to Monte Video and, I assume, regions more favorable to winter sports.  If a few of those snotty little fellow get hypothermia or frost-bite, perhaps that will improve their personalty.

 

This would be a good time for me to begin coding a program I want to try, creating a data file to tell me how many users have visited my file when running the program.

 

Because this will be mainly a text-based program, I won't worry about creating a GUI, although that would also be nice.  In this DEMO, we won't need the FILEDIALOG because there will be only one file to work with, and it will be located in the same folder as the .BAS program.  Let's get to more code…

 

"Hold on, Welopez!"  I felt a sharp rap across the knuckles of my right hand as I reached for the mouse.  There she was, a Worble no less, when I thought they were all on vacation.  Dowdy was not a strong enough word to describe her.  Mrs. Speer, my third grade teacher, had been dowdy, but this matron made Mrs. Speer look like Miss Universe!

 

"Uhh, hello.  Can I help you with something?"

 

"With most of my Worble family skiing in South America, I was getting a little lonely down there in the file library, so I thought I would pop up and see what sort of mischief you were creating to day."

 

"What happened to Phyllie, my FAT Librarian?"  I could hope, couldn't I?

 

"Phyllie is in charge of your File Allocation Table.  We have an organization down here too, you know.  I'm in charge of your data files.  Phyllie is sort of like the hostess or Maitre d' in a fancy restaurant, attractive but not very bright. She knows where everything must be, but it takes an old timer like me, with much more experience to make sure your files are properly handled.  My name is Ophelia Packerschnozz, and we'll have no jokes about that or I'll wrap you on the knuckles again young fella."

 

"Yes, ma'am.  No jokes.  Understood."

 

"Very well, then, you can call me Ms. Ophelia like the junior Worbles do.  Let me take a look at what you've fouled up so far.  Hmmm, not bad for a start.  At least you had the good sense to create a project folder.  All good data clerks must be able to keep their files organized to make it easier to find them when someone needs to use them.  I have an entire folder in one of my filing cabinets, just for WELOPEZ."

 

"A folder just for me?  I feel honored!  Truly, I do!"

 

"Who said anything about a folder named for you?  Don't be silly!  The folder is for Worble Enhanced Learning Oriented Programming – EZ." 

 

"Oh, my mistake," I said somewhat sadly.

 

"I should think so!  Now, what do you intend to do with this data file?  It pays to have some sort of plan before the project gets out of hand and needs to be modified."

 

"I just want to keep track of how many users have visited my file."

 

"What file," Ms. Packerschnozz asked.  "Where is it located?  How do you expect me to find it?"

 

"Well, Ms. Ophelia, I haven't gotten to that part yet.  I've only just started."

 

"Then lets do this right to avoid a program crash the first time you run your program.  You can hard code the name of your file in any program, but sometimes it saves a little typing if you want to hold the file name in a string variable.  Let me begin your code this way."

 

'Your First File Operation

 

myFile$="tracker.txt"  'If the file is in another folder or directory,

                       'you must specify the relative path.

 

ON ERROR GOTO [errorHandler]  'If we get a system error, go to [errorHandler]

OPEN myFile$ FOR INPUT AS #f

INPUT #f, vis  'Get a number from the file.

vis=vis+1  'Add 1 to the value retrieved.

PRINT "You are visitor number "; vis ; " to this file."

CLOSE #f

 

"What's an error handler?  I've never used that command before?"

 

"That's why you have Worbles like me to assist you and correct your code, Welopez.  We're going to work with a data file named "tracker.txt" in this program.  Before you can run the program without an error, you must check to see if you have the data file.  If we tell the program to OPEN myFile$ for INPUT, and the file does not exist, we'll receive an error.  If any error pops up, this code will branch to [errorHandler] and evaluate the error.  For a partial list of possible error codes, see the helpfile topic ON ERROR GOTO [].

 

"The other possibility is your file does exist, in which case the file is opened without error, a value is retrieved using the INPUT statement and incremented by 1.  Then the value is printed to the MAINWIN inside a short message."

 

"Okay, I understand that, Ms. Ophelia, but you haven't explained the [errorHandler] yet, or perhaps I'm just slow today."

 

"Pay more attention, Welopez, do your sleeping on your own time.   In this program, the error routine is located at the end.  This is what the complete program looks like."

 

'Your First File Operation

 

myFile$="tracker.txt"  'If the file is in another folder or directory,

                       'you must specify the relative path.

 

ON ERROR GOTO [errorHandler]  'If we get a system error, go to [errorHandler]

OPEN myFile$ FOR INPUT AS #f

INPUT #f, vis  'Get a number from the file.

vis=vis+1  'Add 1 to the value retrieved.

PRINT "You are visitor number "; vis ; " to this file."

CLOSE #f

 

OPEN myFile$ FOR OUTPUT AS #f

PRINT #f, vis  'Store the new number in the file.

CLOSE #f

END

 

 

WAIT

[errorHandler]  'If the file does not exist, create the file and add the first visitor.

vis=1  'Vis initializes as zero when the program is run.  Add 1 to that value.

OPEN myFile$ FOR OUTPUT AS #f

PRINT #f, vis  'Store this number in the file.

PRINT "You are visitor number "; vis ; " to this file."

CLOSE #f

END

 

"Okay, I get it now.  The first time we run the program, the file does not exist and there is no value to retrieve.  So the error routine sets vis=1, OPEN myFile$ FOR OUTPUT and prints the value of vis to the file.  At the same time we print a message to the user, letting him or her know they are the first visitor, and because of where we've placed the message in the routine, we also know the file has been created and the value saved to the file."

 

"My, you are quick, Welopez.  One day you may even qualify as a retarded Worble."  Come on now, is there any reason she has to make disparaging remarks about me?

 

"If there is no programming error when the program is run, the program retrieves the number of visitors from the file, adds one visitor, and then prints the new value to the file and closes.  That's pretty simple, but how do I know when to use OUTPUT, INPUT, and APPEND?"

 

"You can never use more than one parameter at the same time, Welopez.  Usually you will want to begin by opening a file for INPUT to load values into your program, or into an array where you can work with them.  If you want to save those values to work with another time, first close the file opened for INPUT, and then open the file for OUTPUT so you can print variables to the file.  But, it's important to remember, whenever you open a file for OUTPUT, anything currently in the file is lost and the only values saved are those you write at the close of this session."

 

"Then how can I add another person to a file containing the telephone numbers of all my friends, without having to enter all the data again?"

 

"A computer would not be very useful if you couldn't edit a file, Welopez, so there is also the APPEND parameter.  When you open a file for APPEND, you can add new data to the end of the file without losing any other stored information."

 

"Doesn't the computer get upset if I am opening and closing these files all the time?" I asked.

 

"That's why you have an experienced Worble like me to perform these tasks, Welopez.  Your Windows Operating System creates new files willy-nilly and stores them all over your hard drive.  It saves your preferences, configuration settings, temporary data, and a lot of other useful information.  It's extremely important that you or your program properly close all files before exiting, or the data you have been working with may not be properly saved."

 

"Well, thanks, Ms. Ophelia, I think you've taught me all I need to know about working with data files now and I can begin writing some serious programs."

 

"Oh, you think so?  I may have to require you to stay after class so we can cover INPUT and LINE INPUT.  You may need to retrieve multiple variables and string data and put the information into one or more arrays, or perhaps a multi-dimensioned array to make it easier to work with.  You'll have to learn how to determine the length of a file using the LOF() function and the number of data fields in the file so you can dimension your array or arrays."

 

"Will this take long, Ms. Ophelia?  I really should be having lunch about now."  Besides, arrays with multi-dimensions sounds too much like science-fiction to me.

 

"Okay, you can take a lunch break, Welopez.  While you're stuffing your face, look through the help files and bring to class next time, a 2,000 word essay covering the types of input, LOF(), looping, and arrays.  Look through the sample files provided with JB and see how data is used in other programs.  There is no use inventing the wheel again when you can learn how someone else has accomplished the same task."

 

Sheesh!  Two thousand words on file operations!  I thought I wouldn't have to write any more essays when I got out of school so many decades ago!  I guess I better get on it, if I don't learn how to save and access my data files, Ms. Packerschnozz will probably make me stay after class when I'd rather television later.  Worse, she may not even let me use my computer!