Hi, I had a problem to transfer a binary file to normal text file. I stored 24000 float number in the binary file, so the size of this binary file is eaxct 96000 bytes. I wanna read all these numbers and store them in a text file(6 numbers on each line). I'm a newbie on the fstream class. Here is the test codes. Any suggestion is welcome.
@Duoas
Thanks for your reply,but i still get a few puzzles.
line 13: why a=a+1.1 , isn't a++ ok ?
line 26-28: the comments are my understandings
1 2 3
ins.read( reinterpret_cast <char*> (&floats), sizeof( floats ) ); //try to read whole six floats
gcount = ins.gcount(); //get the actual read characters,i.e. bytes read
count = gcount / sizeof( float ); //calculate the actual floats read
Am i right ? If I am,then i just can not figure out what is line 32 for. if ((count * sizeof( float )) != gcount) returnfalse;
Isn't this if condition guaranteed to be true,cause variable count is calulated via division,and you just perform the reversed operation?
Sure, if you only want to increment by one... it is just an example, after all, and I wanted to see some digits after the decimal point.
Your understandings are correct.
Line 32 checks that you managed to read an entirefloat... if your file is only 10 bytes long then there is something wrong.
This example was really just typed-in and tested in a few minutes.. don't read anything too profound from it. In retrospect, I probably should have put line 32 after line 37.
@Duoas
Thanks for your reply. I tried your method. The codes works now. Instead of declare floats[6], I use "new" to allocate memory for variable "floats". But it doesn't work. After line 27
gcount = ins.gcount();
gcount = 4(not 24). What's the difference between these two methods? Thanks.