DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   How to read data from csv file in an array and parse (http://www.daniweb.com/forums/thread50051.html)

rdubey_jsr Jul 14th, 2006 4:35 am
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..

iamthwee Jul 14th, 2006 5:13 am
Re: How to read data from csv file in an array and parse
 
http://www.daniweb.com/techtalkforum...=read+csv+file

Dogtree Jul 14th, 2006 9:53 am
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.

rdubey_jsr Jul 14th, 2006 11:45 am
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

rdubey_jsr Jul 14th, 2006 11:47 am
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

iamthwee Jul 14th, 2006 11:59 am
Re: How to read data from csv file in an array and parse
 
What language are we using? c or c++?

Dogtree Jul 14th, 2006 12:23 pm
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.
#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));
}

rdubey_jsr Jul 14th, 2006 12:25 pm
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.

iamthwee Jul 14th, 2006 12:39 pm
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?

rdubey_jsr Jul 14th, 2006 1:35 pm
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.


All times are GMT -4. The time now is 12:59 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC