structure get from textfile

1
2
3
4
5
struct Stock{
	string stockTag;
	double currentValue;
	double numberOfshares;
}


my txt file

1
2
3
4
5
6
7
8
9
10
11
GOOG:390:
ADBE:192:
INFY:167:899921:
AAPL:155:
LOGI:130:
ERIC:110:928238:
INFY:899921
ORCL:893487
BEAS:868694
CTSH:685684
NVDA:654743


how can I store the only 3 number integer to the currentvalue that in structure Stock ? the 3 integer is represent currentValue , so if the other item that don't have 3 integer will be ignored automatically , it's hard for me . can someone help? i do a lot research but still can't understand
You can take this sample code as a base for your program. The only you need is to use std::getline to read records of the file and then apply the idea showed in the sample code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
struct Stock
{
	std::string stockTag;
	double currentValue;
	double numberOfshares;
} obj; 

std::string s( "INFY:167:899921:" );
std::istringstream is( s );

std::cout << s << std::endl;

std::string t;
std::string::size_type n = 3;
for ( ; n && std::getline( is, t, ':' ) ; --n );

if ( n == 0 )
{
	obj.currentValue = std::stod( t );
	std::cout << obj.currentValue << std::endl;
}
why u need to clear an

obj

after the structure? and why u define the string s
( "INFY:167:899921:" )

your istringstream is = istream ? because my compiler now using
istream myfile;

and how u declare the size type n = 3 in string ? i thought it's in int ?
I have not understood your questions. I took one record from your text file as an example and showed how you can extract the third item of the record.
You may declare variable n as you like either as int n; or size_t n;.

I think it would be useful if you would compile and try my example supplying required headers.
Last edited on
Topic archived. No new replies allowed.