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

LVL 4
ChizlAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Infinity08Commented:
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 ?
0
jkrCommented:
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

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

Euhm. That should have been "if the write succeeds, it will have written the amount of bytes you specified", and "if the read succeeds, it will have read at most the specified number of bytes".

If the end of the file is reached during the read, then the eof flag will be set, and you can use gcount to get the actual number of bytes that were read :

        http://www.cplusplus.com/reference/iostream/istream/gcount.html
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

ChizlAuthor Commented:
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

0
Infinity08Commented:
>>  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 ?
0
ChizlAuthor Commented:
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!!
0
ChizlAuthor Commented:
gcount was what I was missing..  Thanks
0
Infinity08Commented:
>> MS is screwing with my head again!!

Heh :)
0
evilrixSenior Software Engineer (Avast)Commented:
>> 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
0
ChizlAuthor Commented:
fstream file_out(pEncodedFileName, ios::out);
0
evilrixSenior Software Engineer (Avast)Commented:
Try: fstream file_out(pEncodedFileName, ios::out | ios::binary);
0
evilrixSenior Software Engineer (Avast)Commented:
or: Try: ofstream file_out(pEncodedFileName, ios::binary);
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.