# \[Tutorial\] CSV Files

**URL:** <https://forums.unrealengine.com/t/tutorial-csv-files/5068>\
**Category:** Community & Industry Discussion\
**Tags:** unreal-engine\
**Created:** [May 27, 2014, 4:16am UTC](https://forums.unrealengine.com/t/tutorial-csv-files/5068 "2014-05-27T04:16:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![SaxonRah](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@SaxonRah](https://forums.unrealengine.com/u/SaxonRah)\
**Post date:** [May 27, 2014, 4:16am UTC](https://forums.unrealengine.com/t/tutorial-csv-files/5068/1 "2014-05-27T04:16:34Z")

</div>

**There is a better way! Don’t listen to me. Go to [Iniside’s post](https://forums.unrealengine.com/showthread.php?7243-Tutorial-CSV-Files&p=59755&viewfull=1#post59755)!**

As requested 🙂

You can do something like this to read any file separated by a delimiter, but here we are reading csv files letter by letter separated by a delimiter “,”; line by line in c++

> [@](#):
>
> I like to typedef stuff, if im going to use it allot/complicated bracketing.
> 
> Init:
> 
> ```auto
> 
> /*--------------------INIT--------------------*/
> typedef struct{
> int lineCount;
> int letCount;
> }infoCSV;
> 
> typedef vector<vector<string> > csvVector;
> 
> infoCSV readCSV(istream &input, vector< vector<string> > &output);
> /*--------------------INIT--------------------*/
> 
> ```
> 
> Code:
> 
> ```auto
> 
> /*--------------------CODE--------------------*/
> infoCSV readCSV(istream &input, vector< vector<string> > &output) {
> infoCSV newInfo;
> newInfo.lineCount = 0;
> newInfo.letCount = 0;
> 
> string csvLine;
> while (getline(input, csvLine)) {
> istringstream csvStream(csvLine);
> vector<string> csvColumn;
> string csvElement;
> while (getline(csvStream, csvElement, DELIMITER)) {
> csvColumn.push_back(csvElement);
> newInfo.letCount++;
> }
> output.push_back(csvColumn);
> newInfo.lineCount++;
> }
> return newInfo;
> }
> /*--------------------CODE--------------------*/
> 
> ```
> 
> Usage:
> 
> ```auto
> 
> /*--------------------USAGE--------------------*/
> string fileName = "filename.csv";
> fstream file(fileName, ios::in);
> if (!file.is_open()) {
> exit(1);
> }
> 
> csvVector csvData;
> infoCSV currentCSVInfo;
> currentCSVInfo = readCSV(file, csvData);
> 
> int cX = 0, cY = 0;
> 
> // print out read data to prove reading is working
> for (csvVector::iterator i = csvData.begin(); i != csvData.end(); ++i) {
> for (vector<string>::iterator j = i->begin(); j != i->end(); ++j) {
> vector<string>::iterator iY = find(i->begin(), i->end(), "
> ");
> if (iY != i->end()){
> cY = distance(i->begin(), iY);
> }
> else {
> cY = distance(i->begin(), iY);
> }
> // Check for Values of slot
> if (*j == something1) {
> doSomething1();
> }
> if (*j == something2){
> doSomething2();
> }
> //Sample Code
> //CaptureY += offset;
> //if (CaptureY >= offset * cY){ CaptureY = 0; }
> }
> //Sample Code
> //CaptureX += offset;
> }
> /*--------------------USAGE--------------------*/
> 
> ```
> 
> You could use this for an inventory system for when a player quits the game. I use this to read random levels generated and saved into CSV files real-time;
> 
> Or whatever. Enjoy.

~Rah

---

<div class="post-metadata">

**Author:** ![iniside](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/iniside/32/866315_2.png) [@iniside](https://forums.unrealengine.com/u/iniside)\
**Post date:** [May 27, 2014, 5:51am UTC](https://forums.unrealengine.com/t/tutorial-csv-files/5068/2 "2014-05-27T05:51:09Z")

</div>

> **[A new, community-hosted Unreal Engine Wiki](https://forums.unrealengine.com/t/a-new-community-hosted-unreal-engine-wiki/141494)**
>
> After over a year in maintenance mode, the official Unreal Engine Wiki is now permanently offline. These resources now live on a new community-run Unreal Engine Community Wiki — ue4community.wiki! You will be able to find content from the official...

[https://docs.unrealengine.com/latest/INT/Programming/Gameplay/DataDriven/index.html](https://docs.unrealengine.com/latest/INT/Programming/Gameplay/DataDriven/index.html)

There is already buld in support for csv files and data reading.

Though nice effort, you didn’t need to reinvent a wheel ;).

---

<div class="post-metadata">

**Author:** ![SaxonRah](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@SaxonRah](https://forums.unrealengine.com/u/SaxonRah)\
**Post date:** [May 27, 2014, 2:47pm UTC](https://forums.unrealengine.com/t/tutorial-csv-files/5068/3 "2014-05-27T14:47:32Z")

</div>

> [@iniside;59755](#):
>
> [A new, community-hosted Unreal Engine Wiki - Announcements - Epic Developer Community Forums](https://wiki.unrealengine.com/Using_excel_to_store_gameplay_data_-_DataTables)
> 
> [https://docs.unrealengine.com/latest/INT/Programming/Gameplay/DataDriven/index.html](https://docs.unrealengine.com/latest/INT/Programming/Gameplay/DataDriven/index.html)
> 
> There is already buld in support for csv files and data reading.
> 
> Though nice effort, you didn’t need to reinvent a wheel ;).

I was requested to make a tutorial about the CSV options from my random level gen, I guess it would have been really smart to SEARCH it first, hehe.

Thank you for bringing this to my attention ! 🙂 It’s so much simpler !
