954,132 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

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

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..

rdubey_jsr
Newbie Poster
10 posts since Jul 2006
Reputation Points: 10
Solved Threads: 0
 
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

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.

Dogtree
Posting Whiz in Training
233 posts since May 2005
Reputation Points: 35
Solved Threads: 3
 
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

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

rdubey_jsr
Newbie Poster
10 posts since Jul 2006
Reputation Points: 10
Solved Threads: 0
 

What language are we using? c or c++?

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

>> 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.

#include <sstream>
#include <string>
#include <vector>

using namespace std;

class ParseCSV {
public:
  typedef vector<string>::const_iterator iterator;

  ParseCSV() {}
  ParseCSV(const string& record) { assign(record); }

  void assign(const string& record);

  iterator begin() const { return split.begin(); }
  iterator end() const { return split.end(); }
private:
  string trim(const string& field);

  vector<string> split;
};

string ParseCSV::trim(const string& field)
{
  string::size_type start = field.find_first_not_of(" \t\v");

  return field.substr(start, string::npos);
}

void ParseCSV::assign(const string& record)
{
  stringstream recStream(record);
  string field;

  split.clear();

  while (getline(recStream, field, ','))
    split.push_back(trim(field));
}
Dogtree
Posting Whiz in Training
233 posts since May 2005
Reputation Points: 35
Solved Threads: 3
 
rdubey_jsr



I am using C++. Actually after parsing i have to use it to show in a List Control in my MFC application.

rdubey_jsr
Newbie Poster
10 posts since Jul 2006
Reputation Points: 10
Solved Threads: 0
 

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?

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 
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.

rdubey_jsr
Newbie Poster
10 posts since Jul 2006
Reputation Points: 10
Solved Threads: 0
 

If I showed you how to tokenise a string rather than a char array would that be ok?

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

>> can u give send me some code how to seperate the contents from commas.
That nice convenient class I posted that does exactly what you asked for must not have been what you really wanted. Why are you using char arrays anyway? How archaic!

Dogtree
Posting Whiz in Training
233 posts since May 2005
Reputation Points: 35
Solved Threads: 3
 
>> can u give send me some code how to seperate the contents from commas. That nice convenient class I posted that does exactly what you asked for must not have been what you really wanted. Why are you using char arrays anyway? How archaic!

he/she prolly doesn't even know what a class is.

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 
>> can u give send me some code how to seperate the contents from commas. That nice convenient class I posted that does exactly what you asked for must not have been what you really wanted. Why are you using char arrays anyway? How archaic!



Yes, you have posted a good class but can u give main() function code also, how to call the class and which member function. Means how to read the file (.CSV) and pass in the object of class and get the parsed content as output.

rdubey_jsr
Newbie Poster
10 posts since Jul 2006
Reputation Points: 10
Solved Threads: 0
 

There's no point worrying about classes here. It will prolly just confuse you. Especially if you had to ask how it may be used with main. And don't forget, you have to incorporate this into an MFC application! Things could get ugly if you don't know what you're doing.


So here's a simple example:

#include <iostream>
#include <string>
#include <sstream>
#include <vector>


using namespace std;

int main()
{
    string tmp = "What,the,hell,are,you,say,in,mo,fo";
    
   vector <string> array; 
  
      
   string token;
   istringstream iss(tmp);
   while ( getline(iss, token, ',') )
   {
      array.push_back(token);
      cout << token <<endl;
   }   
    
    cin.get();
}


If you're dead set on using char arrays this may also be of use:- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1061423302&id=1044780608

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 
Yes, you have posted a good class but can u give main() function code also, how to call the class and which member function. Means how to read the file (.CSV) and pass in the object of class and get the parsed content as output.


In that case, you need to ease off on your project and do something closer to your current skill level. If you can't figure out how to use such a simple class, you're not even close to being ready to parse CSV files and write MFC apps.

Dogtree
Posting Whiz in Training
233 posts since May 2005
Reputation Points: 35
Solved Threads: 3
 

There's no point worrying about classes here. It will prolly just confuse you. Especially if you had to ask how it may be used with main. And don't forget, you have to incorporate this into an MFC application! Things could get ugly if you don't know what you're doing. So here's a simple example:

#include <iostream>
#include <string>
#include <sstream>
#include <vector>
 
 
using namespace std;
 
int main()
{
    string tmp = "What,the,hell,are,you,say,in,mo,fo";
 
   vector <string> array; 
 
 
   string token;
   istringstream iss(tmp);
   while ( getline(iss, token, ',') )
   {
      array.push_back(token);
      cout << token <<endl;
   }   
 
    cin.get();
}

If you're dead set on using char arrays this may also be of use:- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1061423302&id=1044780608







Yes, Now its working fine for me. Thanks, for solving my problem........

rdubey_jsr
Newbie Poster
10 posts since Jul 2006
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You