How to read data from csv file in an array and parse

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

Join Date: Jul 2006
Posts: 10
Reputation: rdubey_jsr is an unknown quantity at this point 
Solved Threads: 0
rdubey_jsr rdubey_jsr is offline Offline
Newbie Poster

How to read data from csv file in an array and parse

 
0
  #1
Jul 14th, 2006
Hi friends,

Its my first experience to this forum. can any one help me in reading a csv file into an array and then parse each word seperated by commas. I have csv file which contains rows and columns of record. and i have to read the file into an array and then parse each value seperated by commas.

thanks in advance..
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: How to read data from csv file in an array and parse

 
0
  #2
Jul 14th, 2006
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 232
Reputation: Dogtree is an unknown quantity at this point 
Solved Threads: 3
Dogtree's Avatar
Dogtree Dogtree is offline Offline
Posting Whiz in Training

Re: How to read data from csv file in an array and parse

 
0
  #3
Jul 14th, 2006
What's the extent of your CSV format? Is it just a bunch of fields separated by commas, or can the fields contain commas as well? A full CSV format means you need to do some tricky quotation parsing if a field needs an embedded comma.
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 10
Reputation: rdubey_jsr is an unknown quantity at this point 
Solved Threads: 0
rdubey_jsr rdubey_jsr is offline Offline
Newbie Poster

Re: How to read data from csv file in an array and parse

 
0
  #4
Jul 14th, 2006
Originally Posted by Dogtree
What's the extent of your CSV format? Is it just a bunch of fields separated by commas, or can the fields contain commas as well? A full CSV format means you need to do some tricky quotation parsing if a field needs an embedded comma.
Extent of CSV format is like
"Ted Morris, 121 street Name, Washington,(123)123344,(12)12322"
"John Peter, 123 Mango, Texas, (232)333523,(23)34355"
....................................
....................................
like this .

Now i have to parse each field seperate like
Ted Morris
121 Street Name
..........
........
No. of Rows and columns are not fixed.
Kindly help me
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 10
Reputation: rdubey_jsr is an unknown quantity at this point 
Solved Threads: 0
rdubey_jsr rdubey_jsr is offline Offline
Newbie Poster

Re: How to read data from csv file in an array and parse

 
0
  #5
Jul 14th, 2006
Originally Posted by rdubey_jsr
Extent of CSV format is like
"Ted Morris, 121 street Name, Washington,(123)123344,(12)12322"
"John Peter, 123 Mango, Texas, (232)333523,(23)34355"
....................................
....................................
like this .

Now i have to parse each field seperate like
Ted Morris
121 Street Name
..........
........
No. of Rows and columns are not fixed in this .
Kindly help me
rdubey_jsr
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: How to read data from csv file in an array and parse

 
0
  #6
Jul 14th, 2006
What language are we using? c or c++?
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 232
Reputation: Dogtree is an unknown quantity at this point 
Solved Threads: 3
Dogtree's Avatar
Dogtree Dogtree is offline Offline
Posting Whiz in Training

Re: How to read data from csv file in an array and parse

 
0
  #7
Jul 14th, 2006
>> Extent of CSV format is like
Well that's simple enough. You don't need to do any tricky parsing if there won't be commas embedded in a field.
  1. #include <sstream>
  2. #include <string>
  3. #include <vector>
  4.  
  5. using namespace std;
  6.  
  7. class ParseCSV {
  8. public:
  9. typedef vector<string>::const_iterator iterator;
  10.  
  11. ParseCSV() {}
  12. ParseCSV(const string& record) { assign(record); }
  13.  
  14. void assign(const string& record);
  15.  
  16. iterator begin() const { return split.begin(); }
  17. iterator end() const { return split.end(); }
  18. private:
  19. string trim(const string& field);
  20.  
  21. vector<string> split;
  22. };
  23.  
  24. string ParseCSV::trim(const string& field)
  25. {
  26. string::size_type start = field.find_first_not_of(" \t\v");
  27.  
  28. return field.substr(start, string::npos);
  29. }
  30.  
  31. void ParseCSV::assign(const string& record)
  32. {
  33. stringstream recStream(record);
  34. string field;
  35.  
  36. split.clear();
  37.  
  38. while (getline(recStream, field, ','))
  39. split.push_back(trim(field));
  40. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 10
Reputation: rdubey_jsr is an unknown quantity at this point 
Solved Threads: 0
rdubey_jsr rdubey_jsr is offline Offline
Newbie Poster

Re: How to read data from csv file in an array and parse

 
0
  #8
Jul 14th, 2006
Originally Posted by rdubey_jsr
rdubey_jsr
I am using C++. Actually after parsing i have to use it to show in a List Control in my MFC application.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: How to read data from csv file in an array and parse

 
0
  #9
Jul 14th, 2006
Ok then, you can just do this:


1. Read in the cvs file.
2. Parse the data using the comma as a delimiter.

Which part are you stuck on?
Last edited by iamthwee; Jul 14th, 2006 at 12:40 pm.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 10
Reputation: rdubey_jsr is an unknown quantity at this point 
Solved Threads: 0
rdubey_jsr rdubey_jsr is offline Offline
Newbie Poster

Re: How to read data from csv file in an array and parse

 
0
  #10
Jul 14th, 2006
Originally Posted by iamthwee
Ok then, you can just do this:


1. Read in the cvs file.
2. Parse the data using the comma as a delimiter.

Which part are you stuck on?
I am able to read the .csv file into a char array but unable to parse the same.
can u give send me some code how to seperate the contents from commas.
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



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC