Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ Any faster method to write a large binary file?
    text
    copied!<p><strong>Goal</strong></p> <p>My goal is to <em>quickly</em> create a file from a <em>large</em> binary string (a string that contains only 1 and 0).</p> <p><strong>Straight to the point</strong></p> <p>I need a function that can achieve my goal. If I am not clear enough, please read on.</p> <p><strong>Example</strong></p> <pre><code>Test.exe is running... . Inputted binary string: 1111111110101010 Writing to: c:\users\admin\desktop\Test.txt Done! File(Test.txt) In Byte(s): 0xFF, 0xAA . Test.exe executed successfully! </code></pre> <p><strong>Explanation</strong></p> <ul> <li>First, Test.exe requested the user to input a binary string.</li> <li>Then, it converted the inputted binary string to hexadecimal.</li> <li>Finally, it wrote the converted value to a file called Test.txt.</li> </ul> <p><strong>I've tried</strong></p> <p>As an fail attempt to achieve my goal, I've created this simple (and possibly horrible) function (hey, at least I tried):</p> <pre><code>void BinaryStrToFile( __in const char* Destination, __in std::string &amp;BinaryStr ) { std::ofstream OutputFile( Destination, std::ofstream::binary ); for( ::UINT Index1 = 0, Dec = 0; // 8-Bit binary. Index1 != BinaryStr.length( )/8; // Get the next set of binary value. // Write the decimal value as unsigned char to file. // Reset decimal value to 0. ++ Index1, OutputFile &lt;&lt; ( ::BYTE )Dec, Dec = 0 ) { // Convert the 8-bit binary to hexadecimal using the // positional notation method - this is how its done: // http://www.wikihow.com/Convert-from-Binary-to-Decimal for( ::UINT Index2 = 7, Inc = 1; Index2 + 1 != 0; -- Index2, Inc += Inc ) if( BinaryStr.substr( Index1 * 8, 8 )[ Index2 ] == '1' ) Dec += Inc; } OutputFile.close( ); }; </code></pre> <p><strong>Example of usage</strong></p> <pre><code>#include "Global.h" void BinaryStrToFile( __in const char* Destination, __in std::string &amp;BinaryStr ); int main( void ) { std::string Bin = ""; // Create a binary string that is a size of 9.53674 mb // Note: The creation of this string will take awhile. // However, I only start to calculate the speed of writing // and converting after it is done generating the string. // This string is just created for an example. std::cout &lt;&lt; "Generating...\n"; while( Bin.length( ) != 80000000 ) Bin += "10101010"; std::cout &lt;&lt; "Writing...\n"; BinaryStrToFile( "c:\\users\\admin\\desktop\\Test.txt", Bin ); std::cout &lt;&lt; "Done!\n"; #ifdef IS_DEBUGGING std::cout &lt;&lt; "Paused...\n"; ::getchar( ); #endif return( 0 ); }; </code></pre> <p><strong>Problem</strong></p> <p>Again, that was my fail attempt to achieve my goal. The problem is the speed. It is too slow. It took more than 7 minutes. Are there any method to quickly create a file from a large binary string?</p> <p>Thanks in advance,</p> <p>CLearner</p>
 

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