943,699 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 11206
  • C++ RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Jul 21st, 2009
4

Re: Parsing a CSV file separated by semicolons.

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:
C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  1. one; two;three;four;;six;
Hope this helps.
Featured Poster
Reputation Points: 1140
Solved Threads: 229
Postaholic
Duoas is offline Offline
2,039 posts
since Oct 2007
Jul 23rd, 2009
0

Re: Parsing a CSV file separated by semicolons.

The only problem with your program is that you need to set local for the stringstream object.
C++ Syntax (Toggle Plain Text)
  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?
Reputation Points: 10
Solved Threads: 0
Light Poster
Yaserk88 is offline Offline
27 posts
since Sep 2008
Jul 23rd, 2009
0

Re: Parsing a CSV file separated by semicolons.

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!
Reputation Points: 10
Solved Threads: 0
Light Poster
Yaserk88 is offline Offline
27 posts
since Sep 2008
Jul 23rd, 2009
0

Re: Parsing a CSV file separated by semicolons.

Click to Expand / Collapse  Quote originally posted by Yaserk88 ...
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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,950 posts
since Aug 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Inheritance question
Next Thread in C++ Forum Timeline: How to disable a button without using MFC





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC