LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 11-04-2004, 05:16 AM   #1
taban1
LQ Newbie
 
Registered: Nov 2004
Posts: 2

Rep: Reputation: 0
C++ read csv file row into vector


Hello,
I was wondering what would be the easiest way in c++ to read a line
from a csv file; each row of the file starts with a string and follows with a variable number of numeric (double), ';' separated values.
I'd like to read the string into a string variable and the doubles into
a vector.
Thanks for your help.
 
Old 11-04-2004, 11:26 PM   #2
karlan
Member
 
Registered: Aug 2003
Location: San Francisco, California
Distribution: Slackware
Posts: 158

Rep: Reputation: 30
look up strtok, and strtok_r for threaded programmings. I'll tell you now that it's a slightly confusing command. If you need help with it, just ask. Untill then i'll let you use the million docs written on it.
 
Old 11-08-2004, 05:42 AM   #3
taban1
LQ Newbie
 
Registered: Nov 2004
Posts: 2

Original Poster
Rep: Reputation: 0
Thanks karlan. I think I can handle the string-splitting part even without strtok. What I don't like though, is the whole loop concept
(lines 18 - 22 of the code dump below) - is it really necessary?. I wonder if something like
vector <double> d;
is >> d;
could work instead of the while loop - or would I have to take care of the '>>' operator overloading myself?
Thanks again.

This is the best I could come up with so far:

1 string rowhead;
2 double dval;
3 string row;
4 vector <double> dvals;
5 vector<double>::iterator iter;
6 int pos;

7 row.erase(row.begin(), row.end());
8 ifstream fin("path_to_the_source_file");
9 while(getline(fin, row))
10 {
11 cout << "Input: " << row << "\n";
12 while((pos = row.find(";",0)) != -1)
13 {
14 row.replace(pos, 1, " ");
15 }
16 istringstream is(row);
17 is >> rowhead;
18 while(!is.eof())
19 {
20 is >> dval;
21 if(!is.eof()) dvals.push_back(dval);
22 }
23 cout << "Output: " << rowhead << ";";
24 for(iter = dvals.begin(); iter < dvals.end(); iter++)
25 cout << *iter << ((iter == dvals.end() - 1) ? "\n" : ";");
26 row.erase(row.begin(), row.end());
27 dvals.clear();
28 }
 
Old 11-08-2004, 02:01 PM   #4
karlan
Member
 
Registered: Aug 2003
Location: San Francisco, California
Distribution: Slackware
Posts: 158

Rep: Reputation: 30
try to use file.eof() to signify eof file, don't use the output of getline.

btw. I'm horible with c++ stl, so i'm no help here. sorry
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Culling Data from a CSV file to output in excel jterr02 Programming 2 05-19-2006 04:58 AM
Shell script to read from csv file hendemeg Programming 1 05-11-2004 08:23 PM
csv to fixed-length file roballen Programming 0 03-11-2004 03:12 AM
How do I select a row of info in a database file? 1rulesall Linux - Newbie 6 12-08-2003 08:27 PM
CSV File AMMullan Programming 2 11-10-2003 12:49 AM


All times are GMT -5. The time now is 07:51 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration