By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,136 Members | 1,021 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,136 IT Pros & Developers. It's quick & easy.

How to save these data?

P: n/a
I've written a program in Python to get some data from a website. The
data structure is as follows. Every day there are new data. For each
day, there are many stocks. For each stock, there are two columns of
numbers namely price and volume. The numbers of data point
(price-volume pair) is not known nor equal for each stock.

Now the problem is how I should save the data in text (if you think I
should use database like MySQL, let me know) so that I can use C++
(with STL) to get the data for analysis. And when I get the data, I
usually get them stock by stock.

At the moment, I save the data like this: a folder for each stock is
made with the name being their stock code. Then two subfolders called
price and volume are made. Then for each subfolder, a text file named
by the date contains the price or volume. (For some reasons I didn't
merge these two in a file with a tab to separate the data)

But I still think there is a better way to do it. The crucial thing is
that for each day, I can put the data for all stock in every single day
in a format like this

Stock Name (may contain space) or Stock Code \n
Price \t Volume \n
....
Price \t Volume \n
Stock Name (may contain space) or Stock Code \n
Price \t Volume \n
....
Price \t Volume \n
....
Stock Name (may contain space) or Stock Code \n
Price \t Volume \n
....
Price \t Volume \n

However, this way I don't know how to extract the data for each stock
using C++, particularly using ifstream. Any suggestion is welcome!
Thanks and merry Xmas!

Dec 24 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a

manuhack wrote in message ...
>
But I still think there is a better way to do it. The crucial thing is
that for each day, I can put the data for all stock in every single day
in a format like this

Stock Name (may contain space) or Stock Code \n
Price \t Volume \n
...
Price \t Volume \n
Stock Name (may contain space) or Stock Code \n
Price \t Volume \n
...

However, this way I don't know how to extract the data for each stock
using C++, particularly using ifstream. Any suggestion is welcome!
Thanks and merry Xmas!
Load the file, line by line, into a vector. Then parse the strings from
there.

// #include <string>, <fstream>, <vector>
std::ifstream datin( "testdata.txt" );
if( not datin.is_open() ){ throw "a fit"; }

std::vector<std::stringTmpFile;
for( std::string line; std::getline( datin, line ); ){
TmpFile.push_back( line );
}

std::string tmp = TmpFile.at(0);
// parse 'tmp' using string.substr() and string.find()

// or std::stringstream
int Price(0);
int Volume(0);
std::istringstream data( TmpFile.at(0) );
data >Price;
data >Volume;
Merry Christmas all!
--
Bob R
POVrookie
Dec 24 '06 #2

P: n/a

BobR wrote:
>
Load the file, line by line, into a vector. Then parse the strings from
there.

// #include <string>, <fstream>, <vector>
std::ifstream datin( "testdata.txt" );
if( not datin.is_open() ){ throw "a fit"; }

std::vector<std::stringTmpFile;
for( std::string line; std::getline( datin, line ); ){
TmpFile.push_back( line );
}

std::string tmp = TmpFile.at(0);
// parse 'tmp' using string.substr() and string.find()

// or std::stringstream
int Price(0);
int Volume(0);
std::istringstream data( TmpFile.at(0) );
data >Price;
data >Volume;
Now if I have a line with
StockCode \t Stock name (may have space) \n
I put the whole line in a stringstream. Then I recover the stock code
by using >>. However, I've checked a few books looking for how I could
modify >so that I can use >to get the stock name in a string (not
just the name till the first space). Or is there a better way to do it?

Dec 26 '06 #3

P: n/a

manuhack wrote in message ...
>
Now if I have a line with
StockCode \t Stock name (may have space) \n
I put the whole line in a stringstream. Then I recover the stock code
by using >>. However, I've checked a few books looking for how I could
modify >so that I can use >to get the stock name in a string (not
just the name till the first space). Or is there a better way to do it?
{
std::istringstream SimFileIn( "StockCode \t Stock name (may have space)
\n" );
std::string Code;
std::getline( SimFileIn, Code, '\t' );
SimFileIn.ignore(1); // skip space
std::string Name;
std::getline( SimFileIn, Name ); // defaults to '\n'
std::cout<< Code <<std::endl;
std::cout<< Name <<std::endl;
}
/* - output -
StockCode
Stock name (may have space)
*/

--
Bob R
POVrookie
Dec 26 '06 #4

P: n/a

BobR wrote:
>
{
std::istringstream SimFileIn( "StockCode \t Stock name (may have space)
\n" );
std::string Code;
std::getline( SimFileIn, Code, '\t' );
SimFileIn.ignore(1); // skip space
std::string Name;
std::getline( SimFileIn, Name ); // defaults to '\n'
std::cout<< Code <<std::endl;
std::cout<< Name <<std::endl;
}
/* - output -
StockCode
Stock name (may have space)
*/
Does that mean it's not possible to use the operator >to input the
name and code to Name and Code from the stringstream instead of using
getline?

Dec 26 '06 #5

P: n/a

manuhack wrote in message ...
>
BobR wrote:
>{
std::istringstream SimFileIn( "StockCode \t Stock name (may have
space)
>\n" );
std::string Code;
std::getline( SimFileIn, Code, '\t' );
SimFileIn.ignore(1); // skip space
std::string Name;
std::getline( SimFileIn, Name ); // defaults to '\n'
std::cout<< Code <<std::endl;
std::cout<< Name <<std::endl;
}
/* - output -
StockCode
Stock name (may have space)
*/

Does that mean it's not possible to use the operator >to input the
name and code to Name and Code from the stringstream instead of using
getline?
If you are served a bowl of broth, do you use a fork or a spoon to eat it?
Use the proper tool for the job. What is your obsession with the operator>>?
class Stock{ public: // or struct
std::string Code;
std::string Name;
friend std::istream& operator>>( std::istream &input, Stock &St){
std::getline( input, St.Code, '\t' );
if( ' ' == input.peek() ){ input.ignore();} // skip a space
std::getline( input, St.Name ); // defaults to '\n'
return input;
} // end operator>>(istream&, Stock&)
}; // class Stock

int main(){
std::istringstream SimFileIn( "StockCode \t Stock name (may have space)
\n" );
Stock St1;
SimFileIn >St1;
cout<< St1.Code <<std::endl;
cout<< St1.Name <<std::endl;
}
/* - output -
StockCode
Stock name (may have space)
*/

--
Bob R
POVrookie
Dec 26 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.