Qbasic, Quickbasic/Passing Parameters

Advertisement


Question
QUESTION: Hi Alex, i hope you can give me some idea regarding my problem. Can I pass a parameter coming from the outside of the program? For example I have a program named 'PROGRAM.BAS', can I pass a parameter on this by executing PROGRAM Jojo 1234? Wherein the Jojo and 1234 value will be accepted inside the PROGRAM.BAS and manipulated.

ANSWER: Hi, Jojo,

What you'll want is to use the COMMAND$() built-in array.  I'm not sure if this will work without compiling the program, though.
To call the script, you'd do something like:
qb.exe yourBasicCode.bas arg1 arg2 3

You'll have to play around with the command to get it working right.

You may want to make a command like this:

FUNCTION HasCommandLine( lookFor AS STRING, index AS INTEGER )
 DIM i AS INTEGER
 HasCommandLine = -1
 IF index < 0 THEN
   FOR i = LBOUND( COMMAND$ ) TO UBOUND( COMMAND$ )
     IF LCASE$( COMMAND$( i ) ) = LCASE$( lookFor ) THEN
       HasCommandLine = i
       EXIT FUNCTION
     END IF
   NEXT i
 ELSE
   IF LCASE$( COMMAND$( index ) ) = LCASE$( lookFor ) THEN
     HasCommandLine = index
     EXIT FUNCTION
   END IF
 END IF

END FUNCTION

I hope that helps you out,
-Alex

---------- FOLLOW-UP ----------

QUESTION: Thanks for the help on my first problem Alex. I hope you wouldn't mind if I ask a follow question. For example, I have a file SAMPLE.DAT, what command will I used to check if this file really exist before I use it? In CLIPPER or FOXPRO there is a File() function which check first the presence of the file by returning TRUE or FALSE. Is there an equivalent basic command for this? Thank you and more power.

Answer
Hi, Again,

QBasic doesn't have a built in function to see if a file exists.  There are a few "hacks" to look for a file, though.

FUNCTION FileExists( path AS STRING ) AS INTEGER
 FileExists = NOT 1
 SHELL "DIR /B " + path + " > tmp.txt"
 DIM f AS INTEGER
 f = FREEFILE
 OPEN "tmp.txt" FOR INPUT AS #f
   IF LOF( f ) THEN FileExists = 1
 CLOSE #f
 KILL "tmp.txt"
END FUNCTION

IF FileExists( "c:\randomfile.dat" ) THEN ...
IF NOT FileExists( "SAMPLE.DAT" ) THEN ...

That should work, if not, let me know, and I'll see what can be done,
-Alex

Qbasic, Quickbasic

All Answers


Answers by Expert:


Ask Experts

Volunteer


Alex Barry

Expertise

I have been a qbasic programmer since 2000, creating games, minor libraries and various small programs. I have experience using interrupts, graphics, file input/output, the mouse cursor, and using libraries. I have also learned FreeBASIC, QB64, c/c++, python, lua, php and html.
I do not claim to be an absolute authority in any language, but I don't mind looking things up and learning with you.

Experience

I have been programming in *Basic dialects since 2000, as mentioned in my expertise. After a year of QBasic, I learned C and C++, and dabbled a little in ASM (I don't program in ASM - I literally just played around to see how things work). When QB64 and FreeBASIC were released, I played with those languages. At the time, FreeBASIC offered more functionality and I sided with that language for a while. During that time, while I was learning new languages, that I would see what scripting languages are available, where I took up python and lua. I started to notice a staleness to QB64's development (which I kept tabs on from time to time), and am now trying to be active in it's community and maybe in it's development in the future. Currently, I am only active on the QB64.net forums, but I appear on occasion on FreeBASIC.net's forums as well.

Education/Credentials
Highschool - 2007

©2012 About.com, a part of The New York Times Company. All rights reserved.