Thread: Formatting output to a file.

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    14

    Formatting output to a file.

    Hi,

    I am trying to format data in a flat file in a particular format. The following are the steps.

    STEP 1:

    Data elements in the code.
    Code:
    	string FixedValue;
    	string Milliseconds;
    	string Symbol;
    	string LastTradePrice;
    	string AccumulatedVolumeTrading;
    	string BestBidPrice;
    	string BestAskPrice;
    	string BestBidSize;
    	string BestAskSize;
    	string DataBlock;
    STEP 2
    Convert data type of type double, int and Timestamp into String defined in the data elements.

    STEP 3

    create a input file stream and write the output into the file like the following.

    Code:
    ofstream data_file ("data_file.dat");
    data_file << symbol + "," + BestBidPrice + "," + BestBidSize << endl;

    My question is how to convert double, int and Timestamp to string format efficiently and write it to the file?

    Regards,

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    14
    I have been able to convert the double and int in the following manner.
    Code:
    	char Size[50];
    	char Price[50];
    		BestAskPrice = sprintf(Price, "%g", price); //double
    		BestAskSize = sprintf(Size, "%d", size); //int
    But I just cannot convert timestamp to string. Any help is appreciated.

    Regards,

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Please post a complete program that shows what you are trying to do. As it is I don't know what a timestamp is so I can't tell you how to format it.

    Jim

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    14
    Thanks Jim for your response,
    The code is pretty big, however I am posting all the relevant section. There are 3 code sections, header file, class file (which represent the definition of the header file) and finally the client file.

    if you look at the last code section which is the client, I am trying to convert the Timestamp variable to a string inside the function "onTrade"


    Header file definition

    [file]System.h[/file]

    Code:
    namespace test1 {
    
        class SessionManager;
    
        class SystemApi {
    
        
    	public:
    		typedef unsigned int Timestamp;
            	virtual void onTradeTick(std::string symbol, int size, double price, int totalVolume, std::string 	
    
    			exchangeId, std::string quoteSource, Timestamp timestamp, std::string tradeType);
    
    
            struct Trade
            {
                uint32_t symbolIndex;
                uint32_t size;
                int32_t priceMantissa;
                int8_t  priceExponent;
                uint32_t totalVolume;
                std::string exchangeId;
                std::string quoteSource;
                uint32_t timestamp;
                std::string tradeType;
                char tradeProperties[4];
                uint32_t tradeId;
                bool isHigh;
                bool isLow;
                bool isCancelled;
                bool isCorrected;
                bool isPreviousSessionLastTrade;
            };
            virtual void onTrade(const Trade& trade);
    
    
        }
    
    }
    class file
    [file] System.cc[/file]

    Code:
    void test1::SystemApi::onTradeTick(std::string symbol, 
    					int size, 
    					double price, 
    					int totalVolume, 
    					std::string exchangeId, 
    					std::string quoteSource, 
    					Timestamp timestamp, 
    					std::string tradeType)
    {
    }
    
    void test1::SystemApi::onTrade(const Trade& trade)
    {
        
    	onTradeTick(getSymbol(trade.symbolIndex), 
    			trade.size, 
    			priceToDouble(trade.priceMantissa, trade.priceExponent), 
    			trade.totalVolume, 
    			trade.exchangeId, 
    			trade.quoteSource, 
    			trade.timestamp, 
    			trade.tradeType);
    
    	}
    client file.
    [file]SystemClient.cc[/file]

    Code:
    class SystemApiClientDemo : public SystemApi {
    
    	public:
    
              void onTrade(const Trade &trade);
              void onTradeTick(std::string symbol, 
    				int size, 
    				double price,
                         
    				int totalVolume, 
    				std::string exchangeId,
                         
    				std::string quoteSource, 
    				Timestamp timestamp,
                         
    				std::string tradeType);
    
    
    }
    
    
    void SystemApiClientDemo::onTrade(const Trade &trade)
    {
        std::string symbol;
        map<int, std::string>::const_iterator searchResult = symbolIndexToName.find(trade.symbolIndex);
        if (searchResult != symbolIndexToName.end())
            symbol = symbolIndexToName[trade.symbolIndex];
        int size = trade.size;
        double price = trade.priceMantissa * (double)pow(10.0, trade.priceExponent);
        long totalVolume = trade.totalVolume;
        std::string exchangeId = trade.exchangeId;
        std::string quoteSource = trade.quoteSource;
        Timestamp timestamp = trade.timestamp;
        std::string tradeType = trade.tradeType;
        if(!printQuotes) return;
        std::cout << "onTradeTick: symbol = " << symbol
                  << ", size = " << size
                  << ", price = " << std::showpoint << std::fixed << std::setprecision(4) << price
                  << ", totalVolume = " << totalVolume
                  << ", exchangeId = " << exchangeId
                  << ", quoteSource = " << quoteSource
                  << ", timestamp = " << timestamp
                  << ", tradeType = " << tradeType
                  << std::endl;
    }
    
    //This is where I am trying to convert the timestamp to a string

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    You could use an ostringstream to convert the unsigned int timestamp to a string
    Code:
            std::string tmp;
    	std::ostringstream os;
    	os << num;
    	tmp.os.str());
    Jim

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    14
    Thanks Jim, I have been able to resolve my issue with your suggestion, appreciate your help, please mark this thread as RESOLVED.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  2. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  3. dos game help
    By kwm32 in forum Game Programming
    Replies: 7
    Last Post: 03-28-2004, 06:28 PM
  4. Need help fixing bugs in data parsing program
    By daluu in forum C Programming
    Replies: 8
    Last Post: 03-27-2003, 06:02 PM
  5. Simple File Creation Algorithm
    By muffin in forum C Programming
    Replies: 13
    Last Post: 08-24-2001, 03:28 PM