?
Solved

fstream binary read, byte size response?

Posted on 2008-02-11
12
Medium Priority
?
867 Views
Last Modified: 2010-05-18
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

0
Comment
Question by:Chizl
  • 4
  • 4
  • 3
  • +1
12 Comments
 
LVL 53

Expert Comment

by:Infinity08
ID: 20870485
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
 
LVL 86

Expert Comment

by:jkr
ID: 20870510
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
 
LVL 53

Accepted Solution

by:
Infinity08 earned 1000 total points
ID: 20870541
>> 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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
LVL 4

Author Comment

by:Chizl
ID: 20871073
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
 
LVL 53

Expert Comment

by:Infinity08
ID: 20871083
>>  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
 
LVL 4

Author Comment

by:Chizl
ID: 20871149
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
 
LVL 4

Author Closing Comment

by:Chizl
ID: 31429970
gcount was what I was missing..  Thanks
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 20871164
>> MS is screwing with my head again!!

Heh :)
0
 
LVL 40

Expert Comment

by:evilrix
ID: 20872957
>> 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
 
LVL 4

Author Comment

by:Chizl
ID: 20877736
fstream file_out(pEncodedFileName, ios::out);
0
 
LVL 40

Expert Comment

by:evilrix
ID: 20879214
Try: fstream file_out(pEncodedFileName, ios::out | ios::binary);
0
 
LVL 40

Expert Comment

by:evilrix
ID: 20879219
or: Try: ofstream file_out(pEncodedFileName, ios::binary);
0

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

601 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question