Link to home
Start Free TrialLog in
Avatar of Chizl
ChizlFlag for United States of America

asked on

fstream binary read, byte size response?

I'm using fstream to read a binary file..

Read doesn't' respond with binary bytes it read, so if I allocate a 100bytes and the file it's reading is only 10 bytes, how do I know that based on the read?    I'm encoding/decoding a file and when I encode I need to read it as binary first, but encoding is done based on every 59 bytes of binary read then writes it out as 76 bytes of encoded data, then reads the next 59 bytes..  This will work fine until the last bit of data in the binary file, where it could be only 3 bytes.  I don't want to encode the crap on the end so I need to know what size the read buffer is.

I know I can get the file size up front, then decrement each time and keep track of it myself, but is there way during the read to do the same thing?
char strInput[19*3] = {0};
char* strOutput;
 
while(!file_in.eof()) 
{
	memset(strInput, 0, sizeof(strInput));
	memset(strOutput, 0, sizeof(strOutput));
	file_in.read(strInput, sizeof(strInput));
	ReadBytes = ????
	EncodeBuffer(strInput, ReadBytes, strOutput);
	file_out.write(strOutput, strlen(strOutput));
}

Open in new window

Avatar of Infinity08
Infinity08
Flag of Belgium image

1) First of all, if you're using binary data, it's better to declare your buffers as unsigned char buffers instead of char buffers.


2) This :

        memset(strOutput, 0, sizeof(strOutput));

    won't work, because strOutput is not an array - it's a pointer.


3) If the read succeeds, it will have written the amount of bytes you specified :

        http://www.cplusplus.com/reference/iostream/istream/read.html


4) I assume that the EncodeBuffer function writes ASCII data to the strOutput string, right ?
Just get the file size and then calculate the amount of bytes as
#include <sys/stat.h>
 
struct _stat st;
 
_stat("filename.bin",&st);
 
int remainder = st.st_size % 59;
int count = st.st_size / 59;
 
for (int i = 0; i < count; ++i) 
{
        memset(strInput, 0, sizeof(strInput));
        memset(strOutput, 0, sizeof(strOutput));
        file_in.read(strInput, 59);
        EncodeBuffer(strInput, ReadBytes, strOutput);
        file_out.write(strOutput, 59);
}
 
file_in.read(strInput, remainder);
EncodeBuffer(strInput, ReadBytes, strOutput);
file_out.write(strOutput, remainder);

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Infinity08
Infinity08
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Chizl

ASKER

gcount, was what I kept missing and what I was looking for..  

Infinity08, I'm using an existing encoding class someone created, but I'm converting it to non-MFC:
http://www.codeguru.com/cpp/cpp/algorithms/article.php/c5099/

I was able to convert the decoding pretty simply, however the encoding method I'm struggling with.   And yes, the output is a string, Base64 to be exact..    For some reason though, the saving of this is putting 0x0D 0x0D 0x0A instead of 0x0D 0x0A at the end of each line..  
file_in.read(strInput, sizeof(strInput));
unsigned int iBytesRead = 0;
iBytesRead = file_in.gcount();
EncodeBuffer(strInput, iBytesRead, strOutput);

Open in new window

>>  For some reason though, the saving of this is putting 0x0D 0x0D 0x0A instead of 0x0D 0x0A at the end of each line..  

Can you show the implementation of EncodeBuffer ?
Avatar of Chizl

ASKER

I figured out the problem..
fstream is converting all 0x0A to 0x0D 0x0A..    Since I have \r\n in my code it converts it to \r\r\n    WTF..  lol..   I changed that class to only have /n and it works fine now..     MS is screwing with my head again!!
Avatar of Chizl

ASKER

gcount was what I was missing..  Thanks
>> MS is screwing with my head again!!

Heh :)
>> fstream is converting all 0x0A to 0x0D 0x0A..    
Did you open the file in binary mode?
http://www.cplusplus.com/reference/iostream/fstream/fstream.html
Avatar of Chizl

ASKER

fstream file_out(pEncodedFileName, ios::out);
Try: fstream file_out(pEncodedFileName, ios::out | ios::binary);
or: Try: ofstream file_out(pEncodedFileName, ios::binary);