| | |
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:
Solved Threads: 0
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..
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..
•
•
Join Date: Jul 2006
Posts: 10
Reputation:
Solved Threads: 0
•
•
•
•
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.
"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
•
•
Join Date: Jul 2006
Posts: 10
Reputation:
Solved Threads: 0
•
•
•
•
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
>> 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.
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)
#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)); }
•
•
Join Date: Jul 2006
Posts: 10
Reputation:
Solved Threads: 0
•
•
•
•
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?
can u give send me some code how to seperate the contents from commas.
![]() |
Similar Threads
- read second line or record of csv file (PHP)
- Reading in certain columns from CSV files into array C++ (C++)
- parse a csv file (PLEASE HELP) (Java)
Other Threads in the C++ Forum
- Previous Thread: Dynamically Add Event Handler without using this pointer
- Next Thread: another "segmentation fault"
| Thread Tools | Search this Thread |
api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data database delete desktop developer directshow dll download dynamic encryption error file forms fstream function functions game generator getline givemetehcodez google graph gui homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple news node number output parameter pointer problem program programming project proxy python random read recursion recursive return string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






