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

Recommended Answers

All 16 Replies

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.

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

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

Member Avatar for iamthwee

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

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

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

Member Avatar for 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?

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.

Member Avatar for iamthwee

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

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

Member Avatar for iamthwee

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

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

Member Avatar for iamthwee

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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.