Streamlining File Input and Output in C++: The Use of Streams with Files

file handling in C++

File input and output operations are essential to many C++ programs, but they can be cumbersome and difficult to manage. Fortunately, C++ provides a powerful mechanism for handling these operations: streams. Streams are a high-level abstraction that simplifies the process of reading and writing to files.

In this article, we will explore the use of streams with files in C++ programming, including their advantages, implementation, and examples of their use.

Advantages of Streams

Streams provide several advantages over traditional file input and output operations. One of the most significant advantages is that streams are abstracted from the underlying file system. This means that a program can use the same stream to read from and write to different types of files, without the need for complex file system management. Streams also provide buffering, which can significantly improve performance by reducing the number of system calls required to read or write data.

Implementation of Streams

Standard input/output library in C++ provides a mechanism to implement streams. This library provides a set of classes that can be used to open, read, write, and close files. The library also defines three standard streams: cin, cout, and cerr. These streams are automatically created and initialized when a C++ program is executed and can be used for input and output operations.

Using Streams in C++ Programs

To use streams in a C++ program, the first step is to open the file using an instance of the std::ifstream or std::ofstream class. These classes provide methods for reading and writing to the file. Once the file is open, data can be read from or written to the file using methods such as << and >>. Finally, the file should be closed using the close() method.

Example C++ Program to use streams

Here is an example of a C++ program that uses streams to read and write to a file:

#include <iostream>
#include <fstream>
using namespace std;

int main () {
   ofstream outfile;
   outfile.open("file.txt");
   outfile << "This is testing for ofstream...\n";
   outfile.close();

   ifstream infile;
   infile.open("file.txt");
   string str;
   while (getline(infile, str)) {
      cout << str << endl;
   }
   infile.close();

   return 0;
}

In this example, the program opens a file called “file.txt” in write mode using the std::ofstream class. It then writes a string to the file and reads the contents of the file using the std::ifstream class and prints them to the console using the getline() method.

Example C++ Program to copy input file

This C++ program demonstrates how to use streams to copy the contents of one file to another, while also simultaneously printing the contents to the I/O Stream.

#include <iostream.h>
#include <fstream.h>
#include <process.h>

void main()
{
ifstream infile;
ofstream outfile;
ofstream printer;
char filename[20];

   cout << "Enter the desired file to copy ----> ";
   cin >> filename;

   infile.open(filename, ios::nocreate);
   if (!infile) {
      cout << "Input file cannot be opened.\n";
      exit(1);
   }

   outfile.open("copy");
   if (!outfile) {
      cout << "Output file cannot be opened.\n";
      exit(1);
   }

   printer.open("PRN");
   if (!printer) {
      cout << "There is a problem with the printer.\n";
      exit(1);
   }

   cout << "All three files have been opened.\n";
  
  char one_char;

  printer << "This is the beginning of the printed copy.\n\n";

   while (infile.get(one_char)) {
      outfile.put(one_char);
      printer.put(one_char);
   }

   printer << "\n\nThis is the end of the printed copy.\n";

   infile.close();
   outfile.close();
   printer.close();
}

Output of the C++ Program
(The input file is copied to the file named “COPY”)
(The input file is printed on the printer

Conclusion

Streams are a powerful mechanism for handling file input and output operations in C++ programming. By abstracting the underlying file system and providing buffering, streams simplify the process of working with files and can improve performance. By understanding how to use streams in C++ programs, you can write more efficient and effective file management code.

M. Saqib: Saqib is Master-level Senior Software Engineer with over 14 years of experience in designing and developing large-scale software and web applications. He has more than eight years experience of leading software development teams. Saqib provides consultancy to develop software systems and web services for Fortune 500 companies. He has hands-on experience in C/C++ Java, JavaScript, PHP and .NET Technologies. Saqib owns and write contents on mycplus.com since 2004.
Related Post