Parsing a CSV file separated by semicolons.

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Oct 2007
Posts: 1,953
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Parsing a CSV file separated by semicolons.

 
4
  #11
Jul 21st, 2009
Sorry to respond to this late... but I wanted to post info also...

The getline() function has the obnoxious habit of returning a not good() stream for final blank fields...

For a single blank line at the end of input, that's fine... (there's no record) but for blank fields it makes a difference. You can get past the problem by checking the stream state before getting a line.

For simple CSV files (meaning you cannot use the ';' character [or whatever character you've chosen] in the field value) this is a working example:
  1. #include <deque>
  2. #include <iostream>
  3. #include <sstream>
  4. #include <string>
  5.  
  6. typedef std::deque <std::string> record_t;
  7. typedef std::deque <record_t> table_t;
  8.  
  9. std::istream& operator >> ( std::istream& ins, table_t& table )
  10. {
  11. std::string s;
  12. table.clear();
  13.  
  14. while (std::getline( ins, s ))
  15. {
  16. std::istringstream ss( s );
  17. record_t record;
  18. std::string field;
  19. bool final = true;
  20.  
  21. while (std::getline( ss, field, ';' ))
  22. {
  23. record.push_back( field );
  24. final = ss.eof();
  25. }
  26. if (!final)
  27. record.push_back( std::string() );
  28.  
  29. table.push_back( record );
  30. }
  31.  
  32. return ins;
  33. }
This will allow you to read all seven fields in a record like:
  1. one; two;three;four;;six;
Hope this helps.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 24
Reputation: Yaserk88 is an unknown quantity at this point 
Solved Threads: 0
Yaserk88 Yaserk88 is offline Offline
Newbie Poster

Re: Parsing a CSV file separated by semicolons.

 
0
  #12
Jul 23rd, 2009
Originally Posted by Ancient Dragon View Post
The only problem with your program is that you need to set local for the stringstream object.
  1. while (getline(infile, line))
  2. {
  3. stringstream strstr(line);
  4. string word = "";
  5. strstr.imbue(locale("german_germany.1252"));
  6. while (getline(strstr,word, ';'))
  7. all_words.push_back(word);
  8. }

Attached is the output file I got.
Hi Ancient Dragon. I see that this makes sense, but the commas in the numbers are not being replaced with decimals. I think the reason for this is that things are still in strings and I need to convert them to integer and float values.

For instances when I do all_words[0] + all_words[2] (all_words[0]=2 and all_words[2]=31), I get 231.

How do I convert each indviudal value of my file so that I can specify it either as int, float, or string?
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 24
Reputation: Yaserk88 is an unknown quantity at this point 
Solved Threads: 0
Yaserk88 Yaserk88 is offline Offline
Newbie Poster

Re: Parsing a CSV file separated by semicolons.

 
0
  #13
Jul 23rd, 2009
I will actually mark this thread as solved and post it as a different problem, because it may be useful for other people. Thanks for the help everyone!
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,578
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1486
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Parsing a CSV file separated by semicolons.

 
0
  #14
Jul 23rd, 2009
Originally Posted by Yaserk88 View Post
How do I convert each indviudal value of my file so that I can specify it either as int, float, or string?
Simple substitution -- call find() to locate the comma and replace it with a period.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC