Formatting output to a flat 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.
1
2
3
4
5
6
7
8
9
10
11

	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.

1
2
3
4

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

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


I have been able to convert the double and int in the following manner.
1
2
3
4
5

	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,
...wait, are you using C or C++?

In C++, use the stream class's features:
1
2
3
#include <fstream>
#include <iomanip>
#include <iostream> 
1
2
3
double BestBidPrice = 12.50;
ofstream data_file( "data_file.dat" );
data_file << fixed << setprecision( 2 ) << BestBidPrice;

What is a Timestamp? You need to first convert it to a std::string and then write it to file. To convert it, you need to know something about it.

Hope this helps.
Thanks Duoas 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]

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
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]

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
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]

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
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
Solution:

1
2
3
4
        std::string tmp;
	std::ostringstream os;
	os << num;
	tmp.os.str());
Please mark this post as resolved.
Topic archived. No new replies allowed.