2

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

Thorvall
  • 87
  • 1
  • 8

3 Answers3

1

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];
Slava
  • 43,454
  • 1
  • 47
  • 90
0

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

Thorvall
  • 87
  • 1
  • 8
  • I disagree. You already have a stream, just use it. If you need to format the double, [do it the string way](http://stackoverflow.com/questions/11989374/floating-point-format-for-stdostream). I believe it is more readable that way. – PatrickV Oct 18 '13 at 15:34
  • Hi Patrick. Tried to implement your suggestion, but got compeling errors when trying the following – Thorvall Oct 20 '13 at 07:56
  • error C3867: 'std::basic_ostream<_Elem,_Traits>::write': function call missing argument list; use '&std::basic_ostream<_Elem,_Traits>::write' to create a pointer to member with [ _Elem=char, _Traits=std::char_traits ] error C2296: '<<' : illegal, left operand has type 'std::basic_ostream<_Elem,_Traits> &(__thiscall std::basic_ostream<_Elem,_Traits>::* )(const _Elem *,std::streamsize)' with [ 1_Elem=char, _Traits=std::char_traits ] error C2297: '<<' : illegal, right operand has type 'std::ios_base &(__cdecl *)(std::ios_base &)' – Thorvall Oct 20 '13 at 08:07
  • pricingOutputFile.write << std::fixed << std::setw( 11 ) << std::setprecision( 6 ) << std::setfill( '0' ) << rowMin; – Thorvall Oct 20 '13 at 08:08
0

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.

PatrickV
  • 2,057
  • 23
  • 30
  • Thanks. Did remove std::setfill('0') & std::fixed since the two 'settings' created problems with negative numbers ie: 00-1.00000 But else it does the job well! – Thorvall Oct 21 '13 at 19:58