Home arrow C++ arrow Page 4 - File Handling and Streams in C++

File Handling and Streams in C++

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.

Author Info:
By: Gabor Bernat
Rating: 4 stars4 stars4 stars4 stars4 stars / 7
April 07, 2009
  1. · File Handling and Streams in C++
  2. · Basic Tasks
  3. · The Mode Flags
  4. · The Binary Flag

print this article

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



// 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




// 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() ;


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.

blog comments powered by Disqus

- Intel Threading Building Blocks
- Threading Building Blocks with C++
- Video Memory Programming in Text Mode
- More Tricks to Gain Speed in Programming Con...
- Easy and Efficient Programming for Contests
- Preparing For Programming Contests
- Programming Contests: Why Bother?
- Polymorphism in C++
- Overview of Virtual Functions
- Inheritance in C++
- Extending the Basic Streams in C++
- Using Stringstreams in C++
- Custom Stream Manipulation in C++
- General Stream Manipulation in C++
- Serialize Your Class into Streams in C++

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials