943,749 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 37219
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Jul 14th, 2006
0

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

Expand Post »
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..
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
rdubey_jsr is offline Offline
10 posts
since Jul 2006
Jul 14th, 2006
0

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

Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Jul 14th, 2006
0

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

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.
Reputation Points: 35
Solved Threads: 3
Posting Whiz in Training
Dogtree is offline Offline
232 posts
since May 2005
Jul 14th, 2006
0

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

Quote 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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
rdubey_jsr is offline Offline
10 posts
since Jul 2006
Jul 14th, 2006
0

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

Quote 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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
rdubey_jsr is offline Offline
10 posts
since Jul 2006
Jul 14th, 2006
0

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

What language are we using? c or c++?
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Jul 14th, 2006
0

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

>> 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.
C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 35
Solved Threads: 3
Posting Whiz in Training
Dogtree is offline Offline
232 posts
since May 2005
Jul 14th, 2006
0

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

Quote 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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
rdubey_jsr is offline Offline
10 posts
since Jul 2006
Jul 14th, 2006
0

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

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.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Jul 14th, 2006
0

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

Quote 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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
rdubey_jsr is offline Offline
10 posts
since Jul 2006

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: Dynamically Add Event Handler without using this pointer
Next Thread in C++ Forum Timeline: another "segmentation fault"





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


Follow us on Twitter


© 2011 DaniWeb® LLC