How to save these data?

Discussion in 'C++' started by manuhack, Dec 24, 2006.

  1. manuhack

    manuhack Guest

    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!
     
    manuhack, Dec 24, 2006
    #1
    1. Advertising

  2. manuhack

    BobR Guest

    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!
    --
    Bob R
    POVrookie
     
    BobR, Dec 24, 2006
    #2
    1. Advertising

  3. manuhack

    manuhack Guest

    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::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?
     
    manuhack, Dec 26, 2006
    #3
  4. manuhack

    BobR Guest

    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
     
    BobR, Dec 26, 2006
    #4
  5. manuhack

    manuhack Guest

    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?
     
    manuhack, Dec 26, 2006
    #5
  6. manuhack

    BobR Guest

    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
     
    BobR, Dec 26, 2006
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. user
    Replies:
    1
    Views:
    782
    Bruce Barker
    Apr 4, 2005
  2. Arjen Hoekstra
    Replies:
    0
    Views:
    576
    Arjen Hoekstra
    Aug 2, 2005
  3. Shi Jin
    Replies:
    3
    Views:
    424
    Bryan Bullard
    Sep 10, 2003
  4. teo
    Replies:
    1
    Views:
    476
  5. Replies:
    4
    Views:
    358
    osmium
    Mar 5, 2007
Loading...

Share This Page