12

The below code is writing unreadable characters to the text file:

int main () 
{
    ofstream myfile ("example.txt");

    if (myfile.is_open())
    {
        double value       = 11.23444556;
        char   *conversion = (char *)&value;
        strcat (conversion, "\0");

        myfile.write (conversion, strlen (conversion));
        myfile.close();
    }   

    return 0;
}

I want to see the actual number written in the file :( Hints please.

EDIT Seeing the answers below, I modified the code as:

int main () 
{
    ofstream myfile ("example.txt");

    if (myfile.is_open())
    {
        double value       = 11.23444556;

        myfile << value;
        myfile.close();
    }   

    return 0;
}

This produces the putput: 11.2344 while the actual number is 11.23444556. I want the complete number.

Editing the post to notify everyone: The unreadable characters are due to ofstream's write function:

This is an unformatted output function

This quote is from: http://www.cplusplus.com/reference/iostream/ostream/write/

Aquarius_Girl
  • 21,790
  • 65
  • 230
  • 411
  • 3
    @Anisha - The default is to display 6 digits, if you want a higher precision, you can do `myfile << setprecision(10) << value;` to get 10 digits in the number. – Bo Persson Jun 03 '11 at 11:15
  • 2
    @Bo Persson: to get 10 digits in the number, `std::fixed` is also necessary, or else the trailing zeros willn't get printed. – Nawaz Jun 03 '11 at 11:21
  • 1
    Casting a double like that, other than wrong in this case, violates strict aliasing rule, so one way or the other your code wouldn't have worked. – King_DuckZ Jun 03 '11 at 12:35
  • @King_DuckZ So what's the correct way of casting? – Aquarius_Girl Jun 03 '11 at 12:40
  • 1
    You need to make a union with double and the type you want to cast to, or use memcopy. The latter on recent compilers should be optimized away and the resulting code would be exactly what you expect, but formally correct. Check this out: http://stackoverflow.com/questions/4163126/c-dereferencing-type-punned-pointer-will-break-strict-aliasing-rules-warning – King_DuckZ Jun 03 '11 at 12:52

4 Answers4

29

Why don't you simply do this (updated answer after the edit in the question):

 #include <iomanip>

 myfile << std::fixed << std::setprecision(8) << value;
 myfile.close();

Now, you can see the actual number written in the file.

See the documentation of std::setprecision. Note: you have to include the <iomanip> header file.

JROS
  • 17
  • 8
Nawaz
  • 353,942
  • 115
  • 666
  • 851
3

It's easier here to use the stream operators:

#include <iostream>
#include <fstream>
#include <string.h>
using namespace std;

int main () 
{
    ofstream myfile ("example.txt");

    if (myfile.is_open())
    {
        double value       = 11.23444556;

        myfile << value;
        myfile.close();
    }   

    return 0;
}

gives you what you want.

MGwynne
  • 3,512
  • 1
  • 23
  • 35
2

Others suggested better ways but if you really want to do it the pointer way there should be casts to convert char* to double * and vice versa

#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
using namespace std;

int main () 
{
  ofstream myfile ("example.txt");
  if (myfile.is_open())
  {
    double value       = 11.23444556;
    char     *conversion = reinterpret_cast<char *>(&value);
    strcat (conversion, "\0");
    //myfile.write (*conversion, strlen (conversion));
    myfile << *(reinterpret_cast<double *>(conversion));
    myfile.close();
  }
  return 0;
}
Umut Tabak
  • 1,862
  • 4
  • 26
  • 41
0

You can write the value to file like this:

myfile << value;

Using the adress of value as parameter strcat corrupts your stack, since strcat expects a 0-terminated c-string as first argument (and actually allocated space).

maxschlepzig
  • 35,645
  • 14
  • 145
  • 182