DATA Files and File Extensions

By Welopez

Many new users have questions when they begin to work with files. This often arises when they see an unknown file extension. There are many file extensions for files, such as TXT, DAT, JPG, INI, HTM, etc. The file extention tells your computer what application is normally used to open and process your file.

Although most file extensions consist of three letters or less, this is not a limitation. The following program uses .reallyawesomebuttotallyuselessfile as the file extension. That's quite a mouthful, and requires many keystrokes to type in, but it can be used, if necessary. It's also not very descriptive, and the average user will not have a clue what the file is supposed to do.

      'Creating a FILE for DATA by Welo, 04022006

      OPEN "myFile.reallyawesomebuttotallyuselessfile" FOR OUTPUT AS #1 '=== Create a file of random numbers
      FOR k=1 to 500 '=== Put 500 random numbers into the file
          num=INT(RND(0)*10000)+1
          PRINT #1, num
      NEXT k
      CLOSE #1

      '=== The file of random values has been created and saved.
      OPEN "myFile.reallyawesomebuttotallyuselessfile" FOR INPUT AS #1 '=== Count lines in the file
      WHILE EOF(#1)=0
          count=count+1 '=== Increment the counter for each line counted
          INPUT #1, dummy '=== Dummy signifies numerical input. Dummy$ could also be used
      WEND
      CLOSE #1

      PRINT "There are "; count; " values in the file."

      DIM myArray(count) '=== DIM an array for the numbers

      OPEN "myFile.reallyawesomebuttotallyuselessfile" FOR INPUT AS #1 '=== Load the array with numbers
      FOR k=1 to count
          INPUT #1, myArray(k)
      NEXT k
      CLOSE #1
      PRINT "The array has been loaded with "; count; " values."

      begin=TIME$("ms") '=== Begin timing the sort
      doOrder=delSort(count) '=== Call the function to sort the array
      finish=TIME$("ms") '=== Finish timing the sort

      FOR k=1 to count
          PRINT myArray(k) '=== Print the sorted array to the MAINWIN
      NEXT k
      PRINT "Sorting the array required "; finish-begin; " milliseconds."

      END

      FUNCTION delSort(count) '=== Selection SORT function courtesy of Del
      FOR i= 1 TO count
          FOR j=1 TO (count-1)
              IF myArray(j) > myArray(j+1) THEN
              myArray(0)=myArray(j+1)
              myArray(j+1)=myArray(j)
              myArray(j)=myArray(0)
              END IF
          NEXT j
      NEXT i
      END FUNCTION

You can copy and paste the program to your IDE (but watch for line breaks!) and run it. The program will automatically create myFile.totallyawesomebuttotallyuselessfile and save it to your disk. Next it will open the file and count the lines, using the line count to DIM an array to be loaded and sorted. When the array has been sorted, it will be printed to the MAINWIN, and the time needed for sorting will be calculated and displayed.

When you write your GollyGeeWhizBang program, you can use any file extension which suits you, but it is generally best to use one which will not conflict with any programs currently existing on your hard drive, and to use a file extension which is simple, as well as descriptive.

Just Basic makes creating files easy. Simply type OPEN "myFile.ext" FOR OUTPUT AS #fil and the file will be created in your current folder. When you open an existing file for output, the current contents of the file will be replaced with the new data you write to the file. To avoid losing the contents of an important data file, use OPEN "myFile.ext" FOR APPEND AS #fil, which will append the data being written to the end of the file, without replacing the current data.

To read data from a file, you must OPEN "myFile.ext" FOR INPUT AS #fil, then use the proper LINE INPUT or INPUT statement. See the help file for definitions.

Working with files is easy. When you open a file for output, if it does not exist, it will be created for you. Optionally, you can use the FILEDIALOG statement to specify the types of files which will be displayed in the FILEDIALOG choices. See the help file topic FILEDIALOG for proper use.

Files may also be opened FOR RANDOM and FOR BINARY, more about that another time. Until then, I'm going to use TXT or DAT for my data files... it's much easier to type