I recently had a support mail request asking how to load and process CSV files in DarkBASIC Professional. So while I was replying I thought I'd throw together a decent little tutorial on the subject.
Requirements:
DarkBASIC Professional
Enhancement Pack
A CSV File
mfc71.dll
** Note, all required files included in attached Zip file
HERE **
Preparing your project Folder
If you want to do this tutorial manually, without using the attached Zip file as the basis for your project structure, then click on File > New > Project > Give the project a name > Locate the Project Folder > Copy Data.csv and mfc71.dll into the root of that folder.
Special Commands Used
OPEN DATA FILE Filename$, Separator$, File ID
CLOSE DATA FILE File ID
return string = GET DATA FILE CELL( File ID, Column, Row )
return integer = GET DATA FILE ROW COUNT( File ID )
return integer = GET DATA FILE COLUMN COUNT( File ID )
What is a CSV file?
Before we get started, if you are not using the supplied CSV file or want to mess around with making your own. Then you should understand it's basic text structure. It's very simple, a spreadsheet, has cells, referenced by rows and columns. However we are not dealing with a spreadsheet interface, so it won't be neatly layed out for us. Instead, you'll have two types of data (string, and numerical) seperated by comma's. The comma's mark the end of one cell, and the start of a new one. String Data is always enclosed in Quotes "" but as the Open Data File command takes this into account you can forget about it. The numerical data is just entered as normal. The Open Data File command, does all the work for you, all you really need to know is which symbol is used to mark the seperation of cells. If for example, you are writing an RPG, with plenty of script data. Then Comma isn't the best choice for you. Try using @ or another symbol that won't appear in your dialouge. Then make a note of the symbol used and enter it into the Open Data File command. It's that easy! The rest is taken care of for you. When I created my sample CSV file, I used OpenOffice Calc, entered in my datasheet, clicked on File > Save As > Gave it a name and selected Text CSV (.CSV) then clicked on save. Comma was used automatically, so if you use this method you can stick with using the comma.
"Word","Integer","String","Float"
0,-10,"Monday",0.1
2,-9,"Tuesday",0.2
4,-8,"Wednesday",0.3
6,-7,"Thursday",0.4
8,-6,"Friday",0.5
10,-5,"Saturday",0.6
12,-4,"Sunday",0.7
14,-3,"Monday",0.8
16,-2,"Tuesday",0.9
18,-1,"Wednesday",1
20,0,"Thursday",1.1
Writing the program!
In order to make our program easy to read, sensible varible names need to be setup, the best way of doing this is using Data Types, I'm assuming you know how to use these already.
Type CSV
File as String
Separator as String
FileID as Word
Col as Integer
Row as Integer
EndType
Next, just in case you're planning to use Functions to load your CSV file, or call data relating to it, we'll setup a varible called SS (Short for SpreadSheet) as assign it to our CSV DataType.
Now we're going to enter the data we already know. The filename, the separator used and the file ID. Then assign them to easy to remember, and organise names.
SS.File = "Data.csv"
SS.Separator = ","
SS.FileID = 1
Next we're going to open our CSV file using the data we've entered.
Open Data File SS.File, SS.Separator, SS.FileID
Now for the data we don't know! We do really because we made the spreadsheet, but if you want to plugin any CSV file at all, with varying numbers of rows and columns, then this is a good way of setting it up. The following commands report this data back to our program.
SS.Row = GET DATA FILE ROW COUNT(SS.FileID)
SS.Col = GET DATA FILE COLUMN COUNT(SS.FileID)
Now we know how many rows and columns there are, we enter it into our Array.
Dim SpreadSheet(SS.Row, SS.Col) as String
Now we need to read each cell of the CSV file, and reference it by it's value. It's just like using X and Y when placing a dot on the screen. So we'll setup a simple For and Next loop, for R and C, then read the corrasponding cell data into our array.
For R = 0 to SS.Row
For C = 0 to SS.Col
SpreadSheet(R, C) = GET DATA FILE CELL(SS.FileID, C+1, R+1)
Next C
NEXT R
We don't need the CSV file anymore, so lets get rid of it.
Close Data File SS.FileID
Using the same For and Next loop, we can read our array and using the text command, plus a little mathematical wizardy, we can reproduce our CSV spreadsheet on the screen.
For R = 0 to SS.Row
For C = 0 to SS.Col
Text 100 * C, 12 * R, SpreadSheet(R, C)
Next C
NEXT R
And to stop the program ending without us telling it too we add the usual pause and program end.
Please note that this is an array designed purely for showing how easy it is to get the information into memory. If you want to do other extra fancy math stuff to it, then you can convert the data from STRING to Integer or Float using the INT or VAL commands respectively. Setting the program to detect WORD, STRING, INTEGER and FLOAT should be easy enough. You can use a Data Type on the Array to store the different data as well. You can not save CSV files directly with a command like Save Data File because the command doesn't exist. However if you really wanted to do this, then you can using the usual file commands. A few quick experiements of my own show this should be possible. Otherwise these commands are very useful for entering in large amounts of organised preset spreadsheet data. RPG's are an obvious example. You can easily create massive stat lists of items, monsters and more, and alter them simply by editing and exporting a CSV file, which you then plugin. RTS games, and any other Stat driven games would also find this useful for loading in base data.
Source Code
rem ****************************************************************************
rem **** Written by Daniel Charles Foreman for TGC Support purposes ****
rem **** This code is royalty free, you may use it for any purpose ****
rem **** Note that mfc71.dll is required to run this code please place ****
rem **** it in your project folder when compiling, file is contained in zip ****
rem **** Sample CSV file also included please place inside project folder ****
rem **** CSV created with and saved using OpenOffice Calc's Text CSV option ****
rem **** Tested with DarkBASIC Professional 7.5 and Enhancements pack. ****
rem ****************************************************************************
`Setup a Datatype to better organise the CSV data
Type CSV
File as String
Separator as String
FileID as Word
Col as Integer
Row as Integer
EndType
`Ensure SS is reachable in all program functions (good practice not required) as assign it the CSV Datatype
Global SS as CSV
`Parameters that control the CSV file, standard CSV's use commas to spereate cells, this may vary simply replace the comma
SS.File = "Data.csv"
SS.Separator = ","
SS.FileID = 1
`This command opens the CSV file, Filename$, Separator$ and FileID(int)
Open Data File SS.File, SS.Separator, SS.FileID
`These commands report the number of Rows and Columns
SS.Row = GET DATA FILE ROW COUNT(SS.FileID)
SS.Col = GET DATA FILE COLUMN COUNT(SS.FileID)
`Setup our Spread Sheet Array, this will store the CSV data so the user can manipulate each cell in memory.
Dim SpreadSheet(SS.Row, SS.Col) as String
`This idea reads the data in each CSV cell, and saved it to our Spread Sheet Array.
For R = 0 to SS.Row
For C = 0 to SS.Col
SpreadSheet(R, C) = GET DATA FILE CELL(SS.FileID, C+1, R+1)
Next C
NEXT R
`The CSV file has been recored into memory, no point leaving it open now.
Close Data File SS.FileID
`Time to display our Spread Sheet Data to the screen just to prove it's all in memory.
For R = 0 to SS.Row
For C = 0 to SS.Col
Text 100 * C, 12 * R, SpreadSheet(R, C)
Next C
NEXT R
Wait Key
End
Rem Note that all data is stored as STRING, if you need to process the String data into numberical data, you can use
Rem the VAL (for floats) and INT (For Integers, should also work for WORD) to convert the data into variables or datatyped arrays.