Welcome to the fourth part of a ten-part series covering the use of streams with C++. Streams can save you a lot of time and effort that would otherwise be spent on trivial, tedious tasks. In this article, we'll show you how to use streams to improve the data flow of your applications.
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:
using namespace std;
#define BUFFER_SIZE 100000 // how many bytes to read at once
#define ADD_ITEM 100000 // how many additional items to add
// open the file
std::ios_base::out | std::ios_base::app);
// this will result in different random numbers at //different run time
// just add the new characters -> convert to char the ASCI
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() ;
size = number.size();
cout << size << endl; // print the result
inputFile.close(); //close the file
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.