C++ binary file I/O to/from containers (other than char *) using STL algorithms -
Text after "
I am trying a simple test of the binary file I / O copy of the data / copy of the STL copy algorithm Use and see a binary file below:
1 #include & lt; Iostream & gt; # 2 included & lt; Iterator & gt; 3 # included & lt; Fstream & gt; 4 # include & lt; Vector & gt; Add # 5 & lt; Algorithm & gt; 6 7 std using the namespace; 8 9 typingfed study :: ostream_itreter & lt; Double & gt; Oi_t; 10 typingfst std :: istream_iterator & lt; Double & gt; Ii_t; 11 12 At Main () {13 14/15 Stud :: Vector & LT; Double & gt; Vdi; For 16 (int i = 0; i & lt; 20; i ++) 17 {18 double d = rand () / 10,00,000.0; 19 vd.push_back (d); 20} 21 22 / binary file displays Output Output output ("temp.bin", iOS :: binary); 24 per (vd.begin (), vd.end (), oi_t (output, (four *) zero)); 25 output. Close (); 26 27 / one container 28 studs from binary file :: Vector & lt; Double & gt; Vi; 29 ifstream input ("temp.bin", ios :: binary); 30 ii_t ii (input); 31 copies (ii, ii_t (), back_incenter (vi)); 32 input.close (); 33 34 // To output data to verify / compare result for 35 (int i = 0; i & lt; vd.size (); i ++) 36 printf ("% 8.4f% 8.4f \ N ", vd [i], vi [ii]); 37 38 printf ("vd.size () =% d \ tvi.size () =% d \ n", vd.size (), vi.size ()); 39 returns 0; 40}
The resulting output is as follows and both problems, afaik:
1804,2894 1804,2985 846,9309 0, 9312 1681,6928 0,6917 1714,6369 0,6420 1957,7478 0,7542 424,2383 0,2387 7198854 0,8852 1649.7605 0.7660 596.5166 0.5171 1189.6414 0.6410 1025.2024 0.2135 1350.4900 0.4978 783.3687 0.3691 1102.5201 0.5220 2044.8978 0.9197 1967.5139 0.5114 1365.1805 0.1815 1540.3834 0.3830 304.0892 0.0891 1303.4557 0.4600 vd.size () = 20 vi.size () = 20
1) Every double
from binary data read from decimal place The previous information is unavailable. 2) Data has been entangled in the third decimal place (or earlier) and some arbitrary error is being introduced.
Please any help would be appreciated. (I will tell anyone about me in the previous post about this because I have come down in my search)
< P> 1) You must specify a separator (for example a location). The non-decimal part was stuck in the decimal part of the previous number. Casting and faucet should generally be a signal that is incorrect in C ++;)
copy (vd.begin (), vd.end (), oi_t (output, "" ));
question 2)
#include & lt; Iomanip & gt; Output & lt; & Lt; Setprecision (9);
Comments
Post a Comment