Note that there are some explanatory texts on larger screens.

plurals
  1. POconvert doubles from a text file into a binary file
    primarykey
    data
    text
    <p>I have a text file full of numbers (from a molecular dynamics output, should be type <code>double</code>) divided in three columns and with thousands of lines like this:</p> <pre><code> -11.979920 -13.987064 -0.608777 -9.174895 -13.979109 -0.809622 </code></pre> <p>I want to read all the numbers in the file, convert them to type <code>double</code> and then save them into a <code>binary</code> file. I know that binary file is not recommended, but I want it to test compression algorithms with both file formats, i.e text and binary. I have tried with <a href="https://stackoverflow.com/questions/5016262/need-to-convert-txt-file-into-binary-file-in-c">Need to convert txt file into binary file in C++</a> but I am not sure if is converting each number in its whole format: -11.275804 or if it is parsing each individual number: -1,1,2,7,5, etc..</p> <p>Edit: Have been trying to convert a single double to binary and back, there are some problems. Here's the core code</p> <pre><code> if( std::string(argv[1]) == "-2bin") //convert to binary { cout &lt;&lt; "Performing the conversion: Text -&gt; Binary..." &lt;&lt; endl; std::ifstream in(argv[2]); std::ofstream out(argv[3], std::ios::binary); double d; while(in &gt;&gt; d) { cout &lt;&lt; "read a double: "&lt;&lt; d &lt;&lt;endl; out.write((char*)&amp;d, sizeof d); } out.flush(); out.close(); cout &lt;&lt; "Conversion complete!" &lt;&lt; endl; return 0; }else if( std::string(argv[1]) == "-2text" ) //convert to text { cout &lt;&lt; "Performing the conversion: Binary -&gt; Text..." &lt;&lt; endl; std::ifstream in(argv[2], std::ios::binary); std::ofstream out(argv[3]); std::string s; while(in &gt;&gt; s) { cout &lt;&lt; "read a string:" &lt;&lt; s &lt;&lt;endl; out.write((char*)&amp;s, s.length()); } out.flush(); out.close(); cout &lt;&lt; "Conversion complete!" &lt;&lt; endl; return 0; </code></pre> <p>When reading only one double, for example <code>1.23456789</code> the read string is just of length 7</p> <pre><code>read a double: 1.23457 </code></pre> <p>I want it to read until the next 'space' and then do the conversion to double->bin. When doing the binary -> text conversion everything is just broken, I don't know how to handle the binary and convert it to double and then string.</p> <p>Update: Finally I managed to check that the binary conversion is working with <code>od -lF</code> utility, but every each line there is a strange line that I have no idea what it means, it cuts the zero from the first number and the output comes in two columns instead of 3:</p> <pre><code>od -lF composite_of10_2.bin | more 0000000 -4600438323394026364 -4599308401772716498 -11.97992 -13.987064 0000020 -4619713441568795935 -4602017412087121980 -0.608777 -9.174895 0000040 -4599312880039595965 -4617904390634477481 -13.979109 -0.809622 </code></pre> <p>Does this look correctly converted? How should I perform the conversion from binary to double string?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload