Question

I have some C++ code developed by a former employee. I'm trying to clarify/test some of the software results. In an intermediate step, the software saves a 'binary' dat file with results, which is later imported by another part of the software.

My aim is to change this output from 'binary' to human readable numbers.

The output file is defined:

ofstream pricingOutputFile;
double *outputMatrix[MarketCurves::maxAreaNr];
ofstream outputFile[MarketCurves::maxAreaNr];

The write step is this one:

 pricingOutputFile.write((char *)&outputMatrix[area], sizeof(double));

The matrix is filled with 'doubles'

Is there a way to change this to output a human readable file?

I have tried various std::string cout and other methods 'googled' but until now without success.

Tried the suggestion with << but that gave the following error: error C2297: '<<' : illegal, right operand has type 'double'

The sugestions her pushed me on the right track:

sprintf_s(buffer, 10, "%-8.2f", rowPos);
pricingOutputFile.write((char *)&buffer, 10);

Inspiration found at: http://www.tenouk.com/cpluscodesnippet/usingsprintf_s.html

Thanks for the help

Was it helpful?

Solution 2

You can just inline this:

pricingOutputFile << std::fixed
                  << std::setw(11)
                  << std::setprecision(6)
                  << std::setfill('0')
                  << rowMin;

But that is very imperative. I always like to stay declarative as long as possible. One simple way to do this would be:

 void StreamPriceToFile(ofstream & output, const double & price) const
 {
      output << std::fixed
             << std::setw(11)
             << std::setprecision(6)
             << std::setfill('0')
             << price;
 }

 //wherever used
 StreamPriceToFile(pricingOutputFile, rowMin);

But even better (in my opinion) would be something like:

 //setup stream to receive a price
 inline ios_base& PriceFormat(ios_base& io)
 {
      io.fixed(...);
      ...
 }

 //wherever used
 pricingOutputFile << PriceFormat << rowMin;

My C++ is very rusty or I'd fill in PriceFormat.

OTHER TIPS

In this code memory occupied by a double is dumped into a file

pricingOutputFile.write((char *)&outputMatrix[area], sizeof(double));

To produce human readable you need to use overloaded operator << :

pricingOutputFile << outputMatrix[area];

The sugestions her pushed me on the right track:

sprintf_s(buffer, 10, "%-8.2f", rowPos); pricingOutputFile.write((char *)&buffer, 10);

Inspiration found at: http://www.tenouk.com/cpluscodesnippet/usingsprintf_s.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top