How to 'export' a code in BASIC?

Door mcavalcanti

Rookie (20)

afbeelding van mcavalcanti

12-03-2013, 02:20

Hello, my friends. I'm doing a program in BASIC that generates a BASIC code. I would like to know if it is possible to save that generated code. For example, the program generates the following lines:

10 cls
20 print "hello world!"

So:

1. I would like to save these two lines to a file called "hello.bas". Is it possible?

2. By the way, is there a way to check, using BASIC, the existence of a file? Something like the PHP "if (file_exists($filename))".

Best.

Aangemeld of registreer om reacties te plaatsen

Van anonymous

incognito ergo sum (118)

afbeelding van anonymous

12-03-2013, 08:04

In basic it would be something like this:

10 ON ERROR GOTO 50
20 FILES "hello.bas"
30 PRINT "File exists"
40 END
50 IF ERR=53 THEN SAVE "hello.bas": RESUME 40

Van anonymous

incognito ergo sum (118)

afbeelding van anonymous

12-03-2013, 08:57

Cheers for your second question, made me boot my msx at 8am. Thank you for that Big smile

To answer your first question, just type save"hello.bas" and your two lines are saved to the hello.bas file. If you want to load the file into an ASCII editor, you should type save"hello.bas",a. This way you can also use the command merge which adds your current BASIC lines to an existing BASIC file.

To answer your second question, like Meits, I'd use error codes as well. I wouldn't use his listing though, because when the file DOES exist, it prints the file on your screen. Which you probably don't want to.

I'm sure better solutions will come up, but I wrote this one just 30 minutes ago Smile

10 on error goto 30
20 name "hello.bas" as "hello.bas"
30 iferr=53thenprint"File does not exist"
40 iferr=65thenprint"File does exist"
50 resume 60
60 Print"End of program"

Line 20 always generates an error. It's either a 'File not found' (error code 53) or a 'File already exists' (error code 65).

Van gasparrini

Champion (309)

afbeelding van gasparrini

12-03-2013, 10:56

Hello guys,

You can see and export code basic with this programm for Windows from PC

Link: http://hp.vector.co.jp/authors/VA002119/msxviewer.html

Best Regards and Happy MSX !!
(^_^)
Andrea.

Van mcavalcanti

Rookie (20)

afbeelding van mcavalcanti

12-03-2013, 11:26

Wow. Thanks. Two great answers for question 2. But I think I wasn't clear about the question 1. I would like just to create a file with the generated code. The user answer a question, and the program generates a code based on the entered values. For example:

10 CLS
20 INPUT "Type musical notes:";N$
30 ?:? "YOUR CODE:"
40 ?"10 CLS":?"20 PLAY"+CHR$(34)+N$+CHR$(34)
50 ?:? "EXPORT CODE TO A FILE? (Y/N)"

I would like to save the two generated lines to a new file.

Van Poltergeist

Champion (276)

afbeelding van Poltergeist

12-03-2013, 12:09

Then you will have to work with OPEN "filename" [FOR mode] AS #filenumber , write the lines, and then close the file. And now I know have things fade away if you don't work with them: I have no clue what the various modes are...

Van NYYRIKKI

Enlighted (5669)

afbeelding van NYYRIKKI

12-03-2013, 13:35

.
.
.
60 k=instr("YyNn",input$(1)):if k=0 then 50 else if k>2 then ?"BYE!":end
70 open "YOURCODE.TXT" for output as #1
80 ?#1,"10 CLS":?#1,"20 PLAY"+CHR$(34)+N$+CHR$(34):close #1:?"CODE EXPORTED!"

LOAD-command can load your exported program. To save file in ASCII text, use ",A" parameter. ie.
SAVE"MYPROG.TXT",A

Van mcavalcanti

Rookie (20)

afbeelding van mcavalcanti

12-03-2013, 13:50

Hey! I used the Latok method and it's working! Thank you everybody. Below, the results...
Code for check file existence:

10 CLS:S=0
20 ON ERROR GOTO 50
30 FILE$=MID$(STR$(S),2)+".BAS"
40 NAME FILE$ AS FILE$
50 IFERR=53 THEN PRINT "SAVING "+FILE$:SAVE FILE$:END
60 IFERR=65 THEN PRINT FILE$+" ALREADY EXISTS":S=S+1
70 RESUME 20

Code for export a generated code:

10 CLS
20 OPEN"FILE.BAS" FOR OUTPUTAS#1
30 PRINT#1,"10 CLS":PRINT#1,"20 COLOR 15,1":PRINT#1,"30 PLAY"+CHR$(34)+"A"+CHR$(34)
40 CLOSE1

The final code (check file existence and export generated code):

10 CLS:S=0
20 ON ERROR GOTO 50
30 FILE$=MID$(STR$(S),2)+".BAS"
40 NAME FILE$ AS FILE$
50 IFERR=53 THEN OPEN FILE$ FOR OUTPUTAS#1:PRINT#1,"10 CLS":PRINT#1,"20 COLOR 15,1":PRINT#1,"30 PLAY"+CHR$(34)+"A"+CHR$(34):CLOSE1:END
60 IFERR=65 THEN S=S+1
70 RESUME 20

Van NYYRIKKI

Enlighted (5669)

afbeelding van NYYRIKKI

12-03-2013, 14:15

In order to avoid problems, please note that only 2 first characters in BASIC program variable names matter.
FILE$="FOO":FISH$="BAR":?FILE$
BAR
Ok

Van mcavalcanti

Rookie (20)

afbeelding van mcavalcanti

12-03-2013, 14:27

Oh, that's true. Thanks to add this, NYYRIKKI. Smile