getfile from text

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
struct Stock{
	string stockTag;
	double currentValue;
	double numberOfshares;
};

void readStocks( Stock *& );
void showStockDetails( Stock *& );

int main(){

//	char choice;

	Stock *thestock = 0;

	/*cout << "Main Menu:"                    << endl;
	cout << "c: Sort Data by Cost "         << endl;
	cout << "v: Sort Data by Trade Volume " << endl;
	cout << "q: quit "                      << endl;

	cout << "enter choice: "                << endl;
	cin  >> choice;*/

	readStocks( thestock );
	cin.ignore();
	showStockDetails( thestock  );

	getch();
	return 0;
}

void readStocks( Stock *&stocks  ){

	ifstream myfile;
	string fileName;

	cout<<"Enter file to open: ";
	cin>>fileName;
	
	myfile.open( fileName.c_str() );

	string   tempStockTag;
	int        StockSize;
	double tempCurrentValue;
	double tempNumberShares;

	myfile >> StockSize;

	stocks = new Stock [ StockSize ];//Dynamic array

	for( int i = 0 ; i < StockSize ; i++ ){
		myfile >> tempStockTag;
		myfile >> tempCurrentValue;
		myfile >> tempNumberShares;

		stocks[i].stockTag       = tempStockTag;
		stocks[i].currentValue   = tempCurrentValue;
		stocks[i].numberOfshares = tempCurrentValue;
	}
	myfile.close();
	//return StockSize;
}

void showStockDetails( Stock *&stocks  ){

	for( int i = 0 ; i < 5 ; i++ ){
		cout << "Details : " << stocks[i].stockTag << endl;
	}
}


inside my textfile
1
2
3
GOOG	390	939393
ADBE	192	757574
INFY	167	899921


why my program will out debug error ?
the rror is

invalid allocation size bla bla.
Last edited on
What is this line supposed to do? I would check that StockSize has a value before allocating the array.
myfile >> StockSize;



Check out this link on reading ints from text-files: http://www.cplusplus.com/forum/beginner/7785/

If StockSize doesn't have a proper value, then the "stocks"-array can't be allocated :)
Last edited on
ya . i know where my problem and i deleted the Stock size .
but now how i gona to store the details to my different variable?

i don't know what should i do for the looping value.
It would be a good idea to store your structs in an STL container, such as a std::vector, instead of an array. One solution is to read a line at a time of input into a std::string, then extract data from that string and into a Stock struct, then add a copy of that struct onto the end of your container. Look into std::getline() and std::ifstringstream.
can provide example?

by the way , it is possible that i no want to use stringstream because i havent learn that part .
The reference section of this website should have all the examples you'll need. If you want to make do without stringstreams, what you should realise is that streams can be used as conditions - they were designed so that using one where a bool is expected will indicate the state of the stream. You should also realise that the >> operator on an input stream returns a reference to the stream. For example, if you tried reading from an ifstream into your struct like so:

1
2
3
while (myfile >> s.stockTag >> s.currentValue >> s.numberOfshares) {
      //   Loop body
}


The body will only execute if it successfully read into s.numberOfshares (because of the way >> associates). Using that, you should be able to figure out how to read your file without knowing in advance how long it is.
I did not test the code but hope it is correct.

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
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <conio.h>

using namespace std;

struct Stock{
	string stockTag;
	double currentValue;
	double numberOfshares;
};

void readStocks( vector<Stock> & );
void showStockDetails( const vector<Stock> & );

int main(){

//	char choice;

	std::vector<Stock> thestock;
	thestock.reserve( 100 );

	/*cout << "Main Menu:"                    << endl;
	cout << "c: Sort Data by Cost "         << endl;
	cout << "v: Sort Data by Trade Volume " << endl;
	cout << "q: quit "                      << endl;

	cout << "enter choice: "                << endl;
	cin  >> choice;*/

	readStocks( thestock );
	cin.ignore();
	showStockDetails( thestock  );

	getch();
	return 0;
}

void readStocks( vector<Stock> &stocks  ){

	ifstream myfile;
	string fileName;

	cout<<"Enter file to open: ";
	cin>>fileName;
	
	myfile.open( fileName.c_str() );


	Stock s;

	while ( myfile >> s.stockTag >> s.currentValue >> s.numberOfshares )
	{
		stocks.push_back( s );
	}


	myfile.close();
	//return StockSize;
}

void showStockDetails( const vector<Stock> &stocks  ){

	for ( Stock s : stocks )
	{
		cout << "Details : " << s.stockTag << endl;
	}
/* Or
	for ( vector<Stock>::size_type i = 0 ; i < stocks.size() ; i++ ){
		cout << "Details : " << stocks[i].stockTag << endl;
	}
*/
}
Last edited on
why must use vector d? if without vector can it be done?
The problem is that you do not know apriori how many records your file contains.
Last edited on
because im not really use vector since my class havent use the vector function yet . and i hope to learn other use thing because @vlad last time u also teach me by using vector.. and i hard to get understand to it... sorry
Then you should use the file format where the first field is the number of records corresponding to one Stock. In this case you can return to your original cide where you read StockSize at first.
Other possibility is to build a single linked list. For example you can define one more structure

1
2
3
4
5
struct StockNode
{
   Stock *current;
   Stock *next;
};


In this case for each read record of the file you will have to allocate memory for Stock and for StockNode.
Or you can update you structure Stock the following way

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


and use only one this structure that to build a single linked list.
Try this code.

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
#include <iostream>
#include <fstream>
#include <string>
#include <conio.h>

using namespace std;

struct Stock{
	Stock *next;
	string stockTag;
	double currentValue;
	double numberOfshares;
};

Stock * readStocks();
void showStockDetails( const Stock * );

int main(){

//	char choice;

	/*cout << "Main Menu:"                    << endl;
	cout << "c: Sort Data by Cost "         << endl;
	cout << "v: Sort Data by Trade Volume " << endl;
	cout << "q: quit "                      << endl;

	cout << "enter choice: "                << endl;
	cin  >> choice;*/

	Stock *thestock = readStocks();
	cin.ignore();
	showStockDetails( thestock  );

	getch();
	return 0;
}

Stock * readStocks(){

	ifstream myfile;
	string fileName;

	cout<<"Enter file to open: ";
	cin>>fileName;
	
	myfile.open( fileName.c_str() );


	Stock *stocks = NULL;
	Stock **current = &stocks;
	Stock s ;
	s.next = NULL;

	while ( myfile >> s.stockTag >> s.currentValue >> s.numberOfshares )
	{
		*current = new Stock( s );
		current = &( *current )->next;
	}


	myfile.close();

	return stocks;
}

void showStockDetails( const Stock *stocks  ){

	for ( Stock *next = stocks; next; next = next->next )
	{
		cout << "Details : " << next->stockTag << endl;
	}
}
Last edited on
error for
Stock *next = stocks


i delete the const , and then it's only work.

can provide a link that how link list actually work and flow?
if explain too long already , then will disturbing you.

it's work, i just learnt link list last few days ago but not yet master it
Also you should add a function that will delete the list.

1
2
3
4
5
6
7
8
9
10
11
void removeStocks( Stock *stocks )
{
	Stock *next = stocks;

	while ( next )
	{
		Stock *current = next;
		next = next->next;
		delete current;
	}
}
why we should de-allocate the memory ?

I thought that we should only print it out?
@BasicNewbie
error for
Stock *next = stocks

i delete the const , and then it's only work.



It was my typo. You shall not remove const. Instead you shall add const.

1
2
3
4
5
6
7
void showStockDetails( const Stock *stocks  ){

	for ( const Stock *next = stocks; next; next = next->next )
	{
		cout << "Details : " << next->stockTag << endl;
	}
}



@BasicNewbie
why we should de-allocate the memory ?

I thought that we should only print it out?


It is a good behaviour to deallocated all memory that we need not more. Otherwise in general case it can result in a memory leak.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int main(){

//	char choice;

	/*cout << "Main Menu:"                    << endl;
	cout << "c: Sort Data by Cost "         << endl;
	cout << "v: Sort Data by Trade Volume " << endl;
	cout << "q: quit "                      << endl;

	cout << "enter choice: "                << endl;
	cin  >> choice;*/

	Stock *thestock = readStocks();
	cin.ignore();
	showStockDetails( thestock  );
	removeStocka( thestock  );

	getch();
	return 0;
}
Last edited on
I see that your menu contains a suggestion to sort your Stocks. In this case you can use the selection sort with the single linked list defined in your program.
Last edited on
i done my selection sort and bubble sort , because i done with few some example , at earlier for this question i just hard to get with linked list , but when i read the vector.

Vector is easier for me to seperate the 3 part into different variable .

i read some reference and vector is easier , linked list quite hard for me.
because i/m not really understand how it's flow also ..

got any link to provide some explanation to me?
Topic archived. No new replies allowed.