How to Create a Program

 

And it's also as easy as pie. Remember this?


<html><head><title>Hello World!</title></head>
<body>

<p>Hello World!

<p>Huh, <em>that's</em> not so tough. Now let's check out <a href="http://www.yahoo.com/">Yahoo</a>.

</body> </html>


This is the "hello world" HTML example that we saw back in the "How to Create a Web Page" tutorial. Now look at this:

 CLS
 PRINT "Create a HTML file"

 OPEN "test.htm" FOR OUTPUT AS #1

 Q$ = CHR$(34)
 PRINT #1, "<html><head><title>Hello World!</title></head>"
 PRINT #1, "<body>"
 PRINT #1, ""
 PRINT #1, "<p>Hello World!"
 PRINT #1, ""
 PRINT #1, "<p>Huh, <em>that's</em> not so tough. Now let's check out"
 PRINT #1, "<a href=" + Q$ + "http://www.yahoo.com/" + Q$ + ">Yahoo</a>."
 PRINT #1, ""
 PRINT #1, "</body>"
 PRINT #1, "</html>"

 CLOSE #1
 END

This is the QBASIC code to generate that HTML file.

If this was loaded into the QBASIC programming editor that you probably already have, and the program was run, then it would produce the "Hello World!" HTML file as output.

There is a lot you can do with a little programming.

What's Going On

What you are seeing is an example of a Programming Language. The specific language is a dialect of BASIC.

Now, if you stare at the above example for a bit, you can probably see what is going on. The only really difficult part is the ugly stuff with Q$. That is an unfortunate necessity, because BASIC uses the " character as a delimiter, just the same way that HTML uses the < and > characters.

So, we have to work around that, by defining a variable to substitute for the " character. (The Q$ = CHR$(34) statement.)

If you load this program into the QBASIC interpreter that you probably already have (if you have either Windows 3.1 or Windows 95), then you can run it. More importantly, you can start adding little things to it. Just the same way that you learned HTML.

Trying it Yourself

The next step is to create a new directory (folder) somewhere on your hard disk to put the BASIC files that you will be experimenting with. Go do that now.....

Now, scroll back a little and select the example code up there, click on Edit/Copy, start up Notepad, File/New to create a new, blank document, and then Edit/Paste to paste the code into NotePad.

Got it? It should end up looking just like it does on the screen, above. OK, now File/Save As it, giving it some original name like test.bas (When you do the File/Save As, make a note of what directory (folder) that it is being saved in. Maybe save it in the new directory that you just created.)

Now you must open a DOS window. When you get to the DOS prompt, change to the directory that you saved test.bas in. Type QBASIC TEST.BAS (Enter) You should see the following screen:

 

QBasic screen shot

Click on Run/Start The screen should clear and you should see:

 

program results screen shot

If this is what you see, then good, you just ran your first program, which created your first web page. With a program. If not, keep fiddling until you get it working. It has to work.

If you are using Windows 95, it is possible that the above didn't work, because QBASIC wasn't installed on your hard disk. Get your Windows 95 installation CD-ROM disk and insert it in your CD-ROM drive. Ignore the "autostart" install program--just close it. What you need to do is copy:

d:\other\oldmsdos\qbasic.exe
d:\other\oldmsdos\qbasic.hlp

into

c:\windows\command

Where d: is your CD-ROM drive.

While you're at it, copy help.com and help.hlp into \windows\command. This is a DOS help program that will give you any explanations that you would want on any DOS commands. For example, try typing HELP QBASIC at the DOS prompt.

Good. You now have an programming environment.

This QBASIC window is an integrated source code editor (works a lot like most editors that you already use), and a program interpreter. When you give the Start/Run command, it will execute the program that you just entered. If some changes are need, well, then make them and try again. Use Save/As to save the program.

One thing that you can't do with this version of BASIC is to make a free standing .EXE file of your program. For that, you need to (somehow!) acquire Microsoft Quick Basic 4.5 (discontinued), which is a DOS program that works similar to QBASIC, EXCEPT that it can generate true compiled .EXE files. That run just as fast as any C programs. Really neat.

However, don't worry about that. You can get a lot done with the QBASIC that you have. AND one really handy thing for programs that you plan on using yourself, you can eliminate the MASSIVE effort of creating a "user interface," by just coding all the stuff that your program would normally have on the outside (i.e., the user interface) right into the body of the program. Your user interface is the QBASIC editor. A real time saver.

Hint. Press Shift F1 to get online help.

An Alternate Explanation

The program starts and then:

Simple enough, but not too valuable by itself, because you could just create test.htm with Notepad, like we did before. Where a program starts getting valuable is when you use it to process other data. One example is to read in a file, have the program make some changes and then write out a modified version to a new file.

Hmmm.

Well, Show Me a Practical Example Then

OK, Now I'm going to walk you through editing a real live program- one that actually does something valuable. But first we are going to set things up so that you have a productive work enviornment.

Getting set up to work

First, open a new browser window (File/NewBrowser, in Netscape), so that the following exercises won't obscure this screen.

Then open up several MS/DOS windows and change to the directory (folder) that you created for doing this stuff. (If your DOS Window comes up taking the whole screen, then press Alt-Enter to "toggle" between full screen and windowed. We want windowed.)

Optional. IF you are running at a high resolution (800x600, or greater), then it is real handy to have the DOS window that you will be running QBASIC have more lines. You can change this at the DOS prompt: Type MODE ,43 to do this. MODE ,25 to change it back. MODE ,50 to get an even larger display.

Another thing that you can do, once you are in the QBASIC editing window, is to shrink down the worthless "intermediate" window that is at the bottom of the screen. To do this, press the Alt-Number Pad + key a few times. Alt-Number Pad - to grow it back again.

Now you need to get the program. Click on cvt2htm.bas, and then File Save/As to save it on your hard disk. Save it in the correct directory.

You will also need txt2html.txt. Save that too.

(Alternate way of getting them: I put both files in a Zip file. Download cvt2htm.zip and unzip.)

From one of the DOS Window, type DIR. Both of those files should show up. If they do, then we can start. Type QBASIC CVT2HTM.BAS. QBASIC should load, and display the program.

Now switch to the second browser window and File/Open File and open TXT2HTML.TXT (the one that you just downloaded.) It should display looking like a text file, not as HTML.

Working with the program

Switch to the QBASIC window and press Shift-F5. The program should run. NOW, switch to the second browser window (the one that is showing TXT2HTML.TXT) and open TXT2HTML.HTM. This SHOULD display the same text as before, BUT now be formatted as regular HTML and should display like a normal web page. (It should look like this.)

Assuming that it did, then, what we have just done is run a program that converted a regular unformatted ASCII text program into a fully formatted HTML program. To view this, start up NotePad (or WordPad), and then File/Open TXT2HTML.HTM. It should display as fully formatted HTML text. The third line should say something like <!-- Created by CVT2HTM on MM-DD-YYYY HH:MM:SS -->, only with your current date and time. Because you just created it.

What's Going On

I'm moving you along a bit faster than traditional programming tutorials. What most programming texts do is introduce you to all the elements of the language, one at a time, with little tutorials for each.

Well, I'm taking different approach. I'm having you START with an semi-complicated working program. Bad part is that there is more stuff going on in the program than you can understand right off the bat. Good part is that it doesn't matter: what matters is that you have a working program at your fingertips and can start tweaking it to meet your needs.

You are starting with a working program.

And also, most of the stuff there doesn't really need to be changed, anyway. You just jump past that until you get to something that you do want to change.

An Alternate Explanation

The program starts, and then:

  1. We initialize some variables. The only one that you are probably interested in is the one that sets the file name:
    Fin$ = "TXT2HTML.TXT" ' Text File to Convert

    If you change TXT2HTML.TXT to be some other file name, than that is the file that will be converted.

  2. Then a lot of stuff to (sort of) validate that the file name is a valid text file and wrangle an output file name by stripping off the .TXT part and adding .HTM.

  3. When that is done and all the files are opened, we write the beginning of the HTML output file. THAT gets a little bit messy, because I wrote a bunch of stuff to try to see if the first line was a likely candidate to be made into a title, 1. to be used as an HTML TITLE, and 2., to be centered and made large at the beginning of of the BODY proper. I had too much spare time on my hands. Shrug.

  4. When all that ugly stuff is done, we then write the stuff that goes at the beginning of all HTML files.

  5. Then we FINALLY get into processing the rest of the file. Which basically consists of reading a line from the input file, doing any conversions that are needed, and then writing the converted line out into the output file.

    Note that this is the part of the program that does all the work, but is actually the simplest part of all. A lot like life.

    One thing that I made optional is whether or not to convert anything that looks like a valid URL (i.e., starts with http://) into being a clickable link. Sometimes I want to do that, sometimes I don't. This is controlled by the variable:
    
    ConvertURLS = 1  ' 1 = Convert URLS, 0 = Do not convert
    
    

  6. When we are done, we write the stuff that is needed for the ending part of the HTML file, and close all the files.

Note that the above basically describes how you would manually convert a regular text file into HTML.

A few more notes

Data Base Publishing

One really interesting application of this is to extend an existing database program into making it into database publishing. Like making a, say, membership database be translated into HTML, so that it can then be FTPed onto the web.

The general idea is to use your database program like you normally do to enter and edit information. BUT, rather then using the normal print functions to print it on the printer, you export the data, sorted the way that you want, into comma-delimited format, and then create a BASIC program to translate that into HTML.

The real powerful aspect of this approach is that this technique can be extended into generating other forms of output: The various "extended" forms of HTML, Microsoft's Winhelp (which uses ASCII RTF as input), Microsoft's new HTMLHelp, RTF to load into other programs (Word, for example), or typesetting languages, like TeX. Or anything else that you know how to express.

You can have one database generate all the output that you need, in all the formats that you need. I don't believe that there is any commercial application that can do all of the above. At least not in the "costs less than a new car" price range.

And, um, this way is free. And reasonably easy.

(I have another program that I could turn into a tutorial, if anybody is interested. Same idea as the above CVT2HTM.BAS example. Let me know.)

 

Reference

For an example of the types of program that you can do using this approach, see my page of utility programs. All of these programs were written in a manner similar to what I have outlined above, and were generally only one or two evenings work. Mostly.

 

For a slightly less hurried introduction, please read "How Programming Works". This is a Mac-centric approach, so don't be surprised that there is a rather large gap, because Macs have no DOS based QBASIC similar to what we have just used. Macs don't have DOS. Mac users consider that to be an advantage.

This is an excellent article, nevertheless. The programming pseudo-language that he uses is remarkably similar (but not identical) to BASIC, so if you follow this article, then you should have no trouble adapting this to QBASIC, which you DO have. (If you are a Windows user.)

 

IF you feel that you really need to create Windows programs with a real Windows user interface, for some reason, then you might be interested in Microsoft's Visual BASIC. You will find that you will be spending a lot of time creating the Windows user interface. But you can, and it is, indeed, Microsoft's progression beyond QBASIC.

But if you have a real need to do things graphically, then VB is a real good way to go. Or to do more advanced things, like connecting to a SQL client server database, then, clearly, you are beyond QBASIC.

To be continued.... (when I get a little bit more ambition.)

 

Use your browser's back button to return to the Table of Contents. Forward if you go too far back.