Reading in CSV file

I am running into problems reading in a csv file.

These are the errors that I am getting

1
2
3
4
5
6
7
8
$ g++ -Wall -o Summary2 Summary2.cpp
Summary2.cpp: In function `int main(int, char**)':
Summary2.cpp:34: error: invalid conversion from `void*' to `char**'
Summary2.cpp:34: error:   initializing argument 1 of `ssize_t getline(char**, size_t*, FILE*)'
Summary2.cpp:34: error: invalid conversion from `int' to `size_t*'
Summary2.cpp:34: error:   initializing argument 2 of `ssize_t getline(char**, size_t*, FILE*)'
Summary2.cpp:34: error: invalid conversion from `char' to `FILE*'
Summary2.cpp:34: error:   initializing argument 3 of `ssize_t getline(char**, size_t*, FILE*)'


This is the code that I have so far
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

struct Quote {
	string date;
	int stockID;
	string expire;
	int strike;
	char cOrp;
	double  bid;
	double ask;
	double price;
		
};



int main(int argc, char * argv[]) {
	
	fstream in(argv[1]);
	if (!in){
		cout << "Unable to open " << argv[1] << endl;	
	}
	else
		cout << "Processing " << argv[1] << endl;
	
	
    Quote q;

	getline(in, q.date, ',');
	cout << "q.date:" << q.date << endl;
 	getline(in, q.stockID,',');
 	cout << "q.stockID:" << q.stockID << endl;
}



My input file looks like this
1
2
3
4
5

3-Nov-00	9483	18-Nov-00	30	C	16.625	17.25	16.9375
3-Nov-00	9483	18-Nov-00	35	C	11.875	12.5	12.1875
3-Nov-00	9483	18-Nov-00	35	P	0.125	0.375	0.25


When I comment out the second getline the program compiles and runs but when I try and add the second getline I get all of these syntax errors
Last edited on
You should include header <string> and read the description of function getline.
Vlad, I had that in the file but somehow it didn't copy over. It's there now
Ok I have been playing around with stringstream and istringstream and I got this to work. But this seems very inefficient. I use a stringstream to read in each comma separated field as a string. THEN I use istringstream to "convert" string fields that should be numeric into a numeric field .

I would think that there would be a better way.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84

#include <iostream>
#include <string>
#include <fstream>
#include <sstream>

using namespace std;

struct Quote {
	string date;
	int stockID;
	string expire;
	int strike;
	char cOrp;
	double  bid;
	double ask;
	double price;
		
};



int main(int argc, char * argv[]) {
	
	fstream in(argv[1]);
	if (!in){
		cout << "Unable to open " << argv[1] << endl;	
	}
	else
		cout << "Processing " << argv[1] << endl;
	
	// String Variables to read input file
	string date;
	string stockID;
	string expire;
	string strike;
	string cp;
	string bid;
	string ask;
	string price;
	
	// Numeric fields to copy  "numeric" string value to actual numeric values
	
	int stockIdInt;
	int numStrike;
	double numBid;
	double numAsk;
	double numPrice;

	string lineIn;


while(getline(in, lineIn)) {
	stringstream linestr(lineIn);
	if (getline(linestr, date, ',') &&
        getline(linestr, stockID, ',') &&
        getline(linestr, expire, ',') &&
        getline(linestr, strike, ',') &&
        getline(linestr, cp, ',') &&
        getline(linestr, bid, ',') &&        
        getline(linestr, ask, ',') &&
        getline(linestr, price, ',')
       )
		{
	       istringstream ( stockID ) >> stockIdInt;
	       istringstream ( strike ) >> numStrike;
	       istringstream ( bid ) >> numBid;
	       istringstream ( ask ) >> numAsk;
	       istringstream ( price ) >> numPrice;
	
	       cout << "\ndate:" << date <<endl;
	       cout << "stockId:"<< stockIdInt << endl;
	       cout << "expire:"<<  expire << endl;
	       cout << "strike:"<< numStrike << endl;
	       cout << "cp:"<< cp << endl;
	       cout << "bid:"<< numBid << endl;
	       cout << "ask:"<< numAsk << endl;
	       cout << "price:"<< numPrice << endl;
	       cout << "price +10 :"<< numPrice +10 << endl;
		}
	}

}
Topic archived. No new replies allowed.