Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / Tutorial: CSV File Loading and Display

Author
Message
Daniel TGC
Retired Moderator
17
Years of Service
User Offline
Joined: 19th Feb 2007
Location: TGC
Posted: 27th Oct 2010 20:23 Edited at: 27th Oct 2010 20:28
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.



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.



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.


Next we're going to open our CSV file using the data we've entered.


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.


Now we know how many rows and columns there are, we enter it into our Array.


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.


We don't need the CSV file anymore, so lets get rid of it.


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.


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

Attachments

Login to view attachments
Burning Feet Man
16
Years of Service
User Offline
Joined: 4th Jan 2008
Location: Sydney, Australia
Posted: 20th Jan 2011 06:26 Edited at: 21st Jan 2011 21:52
How did I miss this tutorial? Using the techniques you've written here, I should only have to make one pass of my CSV files now.

EDIT: One minor gripe, is that with your method commas can't be used within cells of the CSV file. But I've been told that putting comma's in CSV data is bad practice, so it's not a big problem.

Another issue I had was with the Enhancements.chm file. I couldn't find the commands used in this tutorial listed or discussed anywhere!
noturno
13
Years of Service
User Offline
Joined: 9th Mar 2011
Location:
Posted: 10th Mar 2011 02:51
Hey Daniel TGC,

It is a really must have piece of job, man!
Any chances of you transforming it in a plugin trough that DLL? It would be great!

Login to post a reply

Server time is: 2024-04-27 05:33:13
Your offset time is: 2024-04-27 05:33:13