Reading a file and storing into its respective variables

Hi all,
I have input file which contains a transaction details. Each line have 5 data, date,code,activity,shares,price
for example:
24/12/2010,ABC,BUY,40,15


how do i read it and store it in respective variables say
day=24
month=12
year=2010
code=ABC
activity=BUY
shares=40
price=15



What I have done so far is



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
ex E;
getline(input,E.line,'/');
stringstream(E.line)>>date1.dday;		
getline(input,E.line,'/');
stringstream(E.line)>>date1.dmonth;
getline(input,E.line,',');
stringstream(E.line)>>date1.dyear;
getline(input,E.line,',');
stringstream(E.line)>>E.Ccode;	
getline(input,E.line,',');
stringstream(E.line)>>E.Cactivity;
getline(input,E.line,',');
stringstream(E.line)>>E.Cshares;
getline(input,E.line);
stringstream(E.line)>>E.Cprice;


This code works fine. But is there any simpler way to read and store the variables. Because im storing the values into variables directly declared in the class ex and Date date1. Im not at all using set methods used. for example I have a set method
for shares
1
2
3
4
void ex::set_code(int shares)
{
	Cshares=shares;//Cshares is member variable of class ex;
}


How do i use this set method during the reading function
1
2
getline(input,E.line,',');
stringstream(E.line)>>E.Cshares;
Pass a pointer of your input stream to the set_code method.

EDIT:
I recommend not actually using the input stream in the setters, instead, have a 'load' method, which you pass an input stream to, which will parse the data and stick in the corresponding variables.
Last edited on
Thanks for the advice strongdrink.
What I did now is I declared variables
1
2
3
int day,month,year,shares;
	double price;
	string code,activity,line;

in the main itself and using sstream i stored each values into its variables.
Then I call the set methods of the classes and set it.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
while(!in.eof())
	{	
		getline(in,line,'/');
		stringstream(line)>>day;
		getline(in,line,'/');
		stringstream(line)>>month;
		getline(in,line,',');
		stringstream(line)>>year;
		getline(in,line,',');
		stringstream(line)>>code;
		getline(in,line,',');
		stringstream(line)>>activity;
		getline(in,line,',');
		stringstream(line)>>shares;
		getline(in,line);
		stringstream(line)>>price;
		E.set_share(shares);
		size++;
	
	}	


Will that be efficient?
I myself would read an entire line from the file into a std::string. Then have a "setter" function that takes this string and fills in the data members, from the string using string streams. For example:
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
#include <iostream>
#include <sstream>

using namespace std;

struct Test
{
   int day;
   int month;
   int year;
   string code;
   string activity;
   int shares;
   double price;
   void set_all(const std::string& line);
};



int main()
{
   std::string line = "24/12/2010,ABC,BUY,40,15";

   Test test;

   test.set_all(line);

   cout << test.day << endl;
   cout << test.month << endl;
   cout << test.year << endl;
   cout << test.code << endl;
   cout << test.activity << endl;
   cout << test.shares << endl;
   cout << test.price << endl;

   return(0);
}


void Test::set_all(const std::string& line)
{
   // A character for extracting the delimter.
   char delim;
   istringstream ins(line);
   ins >> day >> delim >> month >> delim >> year >> delim;
   getline(ins, code, ',');
   getline(ins, activity,',');
   ins >> shares >> delim >> price;
}
thanks jlb. thats look better choice
Topic archived. No new replies allowed.