How to save these data?

M

manuhack

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!
 
B

BobR

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::string> TmpFile;
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!
 
M

manuhack

BobR said:
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::string> TmpFile;
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?
 
B

BobR

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)
*/
 
M

manuhack

BobR said:
{
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?
 
B

BobR

manuhack wrote in message ...
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)
*/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top