Tutorial 5 Part 2

We will now look at writing the contents of a string to a text file. This is also very simple to do.

You need to open and close the file as you did in the previous tutorial, though this time make sure you open the file as ReadWrite or DXC will not be able to write to the file. Instead of ReadFile$ you use the following command to write a string to the specified file handle:

Three other important commands you should know are the following:

FileExist("FileName")
Checks for the existence of the specified file. Returns True or False.

FileDelete("FileName")
Deletes the specified file. Returns True or False depending on whether the file was found or not.

FileCreate("FileName")
Creates a new file with the specified name. If a file of this name already exists it will overwrite it. Returns True or False depending on whether the file was created successfully or not.

  • As in the previous tutorial, create a new project with a blank background and enter the commands for initialising the display, including the "Redirect GDI Output to Bank 1" command.

  • We need to add in code for getting some input from the user, appending the input to a string.

  • Next check for the existence of the file "text.txt". If it already exists then delete it first. Now create a new file.

    If a file already exists and you write a string to it, the string will be placed before whatever was already in the file. For this reason, delete and recreate the file if it is found.

  • Once the file is created, write the string to it and close the file.

    After the program has run, check the text file and make sure the text you entered during run time is in the file. If not then compare your code to the code I have created below.

    NOTE: If you do not wish to type the code below you may download or open this text file and simply copy and paste the code into your project.


    //TUTORIAL 5 PART 2

    Global GETSTRING$ //USED FOR STORING THE CONTENTS OF THE FILE
    Global X$
    Global QUIT //SET TO TRUE IF THE LOOP SHOULD BE EXITED

    //DEFINE PROCEDURES
    DefProc INIT_DISPLAY()
    DefProc SAVE_FILE()

    QUIT = False

    Set Key Repeat To 500 //SLOW DOWN THE KEY INPUT

    //CREATE THE SCREEN
    INIT_DISPLAY()

    //PROMPT FOR USER INPUT
    Text( 10,50, "Please enter some text and press return: ") : Update Display

    //REPEAT UNTIL THE USER PRESSES THE RETURN KEY (OR ANY NONE TEXT KEY)
    Repeat

      X$ = Inkey$() //GET INPUT

      If X$ > "" Then //MAKE SURE THE USER HAS ENTERED SOMETHING

        If (X$ >= "a" And X$ <= "z") Or (X$ >= "A" And X$ <= "Z") Or X$ = " " Then //MAKE SURE ITS A LETTER OR SPACE

          GETSTRING$ = GETSTRING$ + X$ //APPEND IT TO OUR STRING
          Text ((Len(GETSTRING$)*10)+10,100,X$) //PRINT IT TO THE SCREEN
          Update Display
          X$ = ""

        Else

          QUIT = True
          Text (10,150, "SAVING...") : Update Display

        EndIf

      EndIf

    Until QUIT = True

    //SAVE THE STRING TO THE FILE
    SAVE_FILE()

    //THE INIT_DISPLAY PROCEDURE CREATES A SCREEN AND A PLANE
    Procedure INIT_DISPLAY()

      Create Map 640,480,1,1 In Bank 50 //CREATE A MAP IN BANK 50
      Set Tile 0,0,1 Using Bank 50 //CREATE A TILE USING A BACKGROUND IMAGE IN BANK 1
      Create Plane 1 Using Bank 50 //CREATE PLANE 1 USING THE MAP IN BANK 50
      ReDirect GDI Output To Bank 1 //ALL GDI OUTPUT (IN THIS CASE TEXT, GOES TO BANK 1)

    EndProc

    Procedure SAVE_FILE()

      Local Y

      If FileExist("text.txt") Then //CHECK THAT THE FILE DOESN'T ALREADY EXIST
        Y=FileDelete("text.txt") //IF IT DOES, THEN DELETE IT AND RECREATE IT
      EndIf

      Y=FileCreate("text.txt")

      //OPEN THE TEXT FILE
      Open File "text.txt" As ReadWrite Using 1 //OPEN FILE IN READ/WRITE MODE

      FileWrite(1,GETSTRING$+"&") //WRITE THE STRING TO THE FILE PLUS OUR END OF CHARACTER

      Close File 1 //CLOSE THE TEXT FILE

      Text (10,150, "SAVED....") : Update Display

      Wait Key

    EndProc

    EndProg


  • DXC unfortunately does not have a proper text input function. This means that you must create one yourself, such as the one I have created in this tutorial. The one in this tutorial is only very basic and cannot accept punctuation or the backspace key. Hopefully this will be improved upon in later releases of DXC.

    Previous Page