0

I have a few files that contain nba teams and their starters, and i need to add this information into my program then do a few tasks with them.

I am currently stuck on being able to parse the files.

The files look like this:

Team Name, Point guard, shooting guard, small forward, power forward, center

For example:

Chicago Bulls, Derrick Rose, Kyle Korver, Luol Deng, Carlos Boozer, Joakim Noah Denver Nuggest, .. etc.

I want to be able to only add the names of the team, point guard, and small forward, and I'm not sure on how to do that.

What i have so far:

 17         ifstream* myIN = new ifstream();
 18         myIN->open(filename.c_str());
 19 
 20         string teamName;
 21 
 22         *myIN >> teamName;
 23         
 24         while(!myIN->eof()){
 25                 TEAM.push_back(teamName);
 26                 //TODO
 27         }      

I am looking into using "find()" to find each comma, but i don't know how to only record the information before and after the first comma, and after the 3rd comma.

Any sort of guidance would be much appreciated.

halflings
  • 1,540
  • 1
  • 13
  • 34
nub
  • 17
  • 1
  • 7

2 Answers2

1

This is a really really common (and luckily for you, simple) case of parsing using tokenizing.

Here, the different fields you need are separated by the same character: ,

Please see this stackoverflow question about tokenizing in C++ for more information.

Also, don't forget to remove unnecessary spaces on your tokenized elements (except the first one)

Community
  • 1
  • 1
halflings
  • 1,540
  • 1
  • 13
  • 34
  • Wow, great! I've never herd of it before, i will give it a try. Thank you for your help! – nub Sep 14 '13 at 00:55
0

Many languages (or their standard libraries) have a split function whose purpose is to tokenize (or split) a delimited string. Unfortunately, the C++ std::string class does not offer a split function.

However, boost::split offers this capability; see this answer and other answers to the question Splitting a string in C++.

By the way, your file format appears to be what is commonly referred to as a CSV File. You may also find this question of interest: How can I read and manipulate CSV file data in C++?

Community
  • 1
  • 1
DavidRR
  • 18,291
  • 25
  • 109
  • 191
  • That is exactly how my file is set up. The last link helped tremendously, thank you! – nub Sep 14 '13 at 04:11
  • @Ey0o - Glad my answer helped! By the way, in addition to [accepting my answer](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work), you might also consider [upvoting my answer](http://meta.stackexchange.com/questions/7237/how-does-reputation-work) as well (that is clicking the upward-pointing triangle icon). And as a user new to StackOverflow, you may also find the entire [FAQ for Stack Exchange sites](http://meta.stackexchange.com/questions/7931/faq-for-stack-exchange-sites) quite informative and helpful. – DavidRR Sep 14 '13 at 17:53