File Handling and Streams in C++ - The Binary Flag
(Page 4 of 4 )
The binary flag may seem a little unnecessary at first. It exists for performance reasons. Windows works much faster in this system, as it does not have to worry about the characters, whitespaces, and so on. Here it just reads a sequence of bits; there is less effort invested, and it is more quickly done. Everyone is happy.
Here is a prime example of this. How do you find out how many characters are inside a file? You need nothing else, just the number of characters, and you need it really fast. The complete solution is below. I used all that we have learned until now, and in addition, the read function.
This is a low-level operating function that lets us read chunks of bytes instead proper strings. However, it is using C arrays instead of strings, so I had to construct one that will serve as a buffer for the application:
#include <iostream>
#include <fstream>
#include <iomanip>
#include <vector>
#include <iterator>
#include <time.h>
using namespace std;
#define BUFFER_SIZE 100000 // how many bytes to read at once
#define ADD_ITEM 100000 // how many additional items to add
int main()
{
// open the file
fstream inputFile("C:In.txt",
std::ios_base::out | std::ios_base::app);
// this will result in different random numbers at //different run time
srand((unsigned)time(0));
// just add the new characters -> convert to char the ASCI
for (unsigned long int i = 0; i < ADD_ITEM; ++i )
{
inputFile << (char) ((rand()%125)+3) ;
}
// prepare the file for opening a different file
// or the same just with different flags
inputFile.clear();
inputFile.close();
// now open for read
inputFile.open( "C:In.txt",
std::ios_base::binary | std::ios_base::in);
inputFile >> noskipws;
string number; // this will hold the last input
// allocate space for the buffer
char* block = (char*) calloc(BUFFER_SIZE+1, sizeof(char));
int size = 0; // first just count the number of reads
while ( inputFile.read( block, BUFFER_SIZE) ) // read
{
block[BUFFER_SIZE] = ''; // assure no false data at end
number = block; // save the block
size++; // increase the read count
}
number.append( block, inputFile.gcount() );//what remains //in the end add also
if(size) //calculate the number of read characters
size = (size-1)*(BUFFER_SIZE )+ number.size() ;
else
size = number.size();
cout << size << endl; // print the result
inputFile.close(); //close the file
}
504049
Press any key to continue . . .

Here is the file size with which I ended up after the completion of the program. As you can see here, the number of characters determine the size of the file on the disk, but this is OS-dependent; if you build your application on this, it will not be portable.
The problem may be absurd, or you may find better solutions (and if you do so, please post it on the blog!), but I think it illustrates where the binary flag comes in handy and shows a good way to read large text files into the memory fast.
Again, thank you for reading through my article. I hope you learned a lot from it; all that remains is to invite you to post on the blog here or join the Devhardware forums and ask your questions. Until next time: Live with Passion!
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |