Formatted I/O, File I/O 


Contents of this section:

  1. Formatted I/O using ios member functions
  2. Formatted I/O using I/O manipulators
  3. Define your own I/O manipulators
  4. File I/O: Writing to a text file
  5. File I/O: Reading from a text file

1. Formatted I/O using ios member functions

    Code Listing: 

// File name: ~ftp/pub/class/cplusplus/IO/IOMember.cpp
// Purpose:   Demonstrating formatted IO using ios member functions

#include <iostream>
using namespace std;

int main()
{
	float num[5] = {1.0, -1.2345, 2350.1, 23.4, 45.34};
	int i;

 	cout.setf(ios::showpos); // show the + sign before positive numbers
  	cout.setf(ios::scientific); // use scientific notation
  	cout.precision(2); // two digits after decimal point
	
	for(i = 0; i < 5; i++)
	{
  		cout.width(20);    // use 10 spaces for the number
		cout.fill('$');    // pad with $
  	    	cout << num[i] << endl;
	}

  return 0;
}

    Running session:

mercury[148]% CC -LANG:std IOMember.cpp -o IOMember
mercury[149]% IOMember
$$$$$$$$$$$+1.00e+00
$$$$$$$$$$$-1.23e+00
$$$$$$$$$$$+2.35e+03
$$$$$$$$$$$+2.34e+01
$$$$$$$$$$$+4.53e+01
mercury[150]% 

2. Formatted I/O using I/O manipulators

Code listing:

// File name: ~ftp/pub/class/cplusplus/IO/IOMember.cpp
// Purpose:   Demonstrating formatted IO manipulators

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

int main()
{
	float num[5] = {1.0, -1.2345, 2350.1, 23.4, 45.34};
	int i;

 	cout << setiosflags(ios::showpos);
	cout << setiosflags(ios::scientific); 
	cout << setprecision(2); 
	
	for(i = 0; i < 5; i++)
	{
  		cout << setw(20);    
  		cout << setfill('$');    
  	    	cout << num[i] << endl;
	}

  return 0;
}

Running session:

mercury[156]% CC -LANG:std IOManip.cpp -o IOManip
mercury[157]% IOManip 
$$$$$$$$$$$+1.00e+00
$$$$$$$$$$$-1.23e+00
$$$$$$$$$$$+2.35e+03
$$$$$$$$$$$+2.34e+01
$$$$$$$$$$$+4.53e+01
mercury[158]% 

3. Define your own I/O manipulators

Code listing:

// File name: ~ftp/pub/class/cplusplus/IO/IOUserManip.cpp
// Purpose:   Demonstrates how to define an IO manipulator function
//            of your own.

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

// function prototype for a user-defined IO manipulator function
ostream &SetHex(ostream &stream);

int main()
{
	int input;
	cout << "Enter an integer: " ;
	cin  >> input;

	cout << "Hexdecimal is: ";
	
	// call the SetHex function so that the input
	// integer will be printed out in hexdecimal
	cout << SetHex << input << endl;
	return 0;
}

// definition of a user defined I/O manipulate function
ostream &SetHex(ostream &stream)
{
	// Set output base to be hexdecimal.
	// Use ios::basefield to make sure that all other
	// field in basefield is cleared before setting it
	// to hex.
	stream.setf(ios::hex,ios::basefield); 
	return stream;
}

Running session:

mercury[200]% CC -LANG:std IOUserManip.cpp -o IOUserManip
mercury[201]% IOUserManip
Enter an integer: 19
Hexdecimal is: 13
mercury[202]% 

 

4. File I/O: Writing to a text file

Code Lising

// File name: ~ftp/pub/class/cplusplus/IO/FileWrite.cpp
// Purpose:   Write to text file

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

int main()
{
	char OutFile[80]; // output file name
	ofstream OutStream;  // open an output stream
	
	cout << "Please enter output file name: " ;
	cin >> OutFile;

	OutStream.open(OutFile, ios::out | ios::trunc);
	
	// make sure file is successfully opened
	if(!OutStream)
	{
		cout << "Error open file " << OutFile << " for writing\n";
		return 1;
	}
	
	// Write the following two lines to file
	OutStream<<"It is easier to resist at the beginning than at the end.\n";
	OutStream <<"                 -- Leonardo da Vinci" << endl;

	OutStream.close(); // close output stream

	cout << "Content written to " << OutFile <<".\n";

	return 0;

}

Running Session

mercury[298]% CC -LANG:std FileWrite.cpp -o FileWrite
mercury[299]% FileWrite
Please enter output file name: out.txt
Content written to out.txt.
mercury[300]% 

5. File I/O: Reading from a text file

Code Lising

// File name: ~ftp/pub/class/cplusplus/IO/FileRead.cpp
// Purpose:   Read the content of a text file and print it
//            out on the screen

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

int main()
{
	char InFile[80];  // input file name
	char ch;
	
	ifstream InStream;

	cout << "This program reads the content of a file and ";
	cout << "prints it out on the screen." << endl;

	cout << "Enter input file name: " ;
	cin >> InFile;

	// Open file for input
	// in.open(fin); also works
	InStream.open(InFile, ios::in);

	// ensure file is opened successfully
	if(!InStream)
	{
		cout << "Error open file " << InFile << endl;
		return 1;
	}
	
	cout << "Here is the content of " << InFile << ":\n";
	// Read in each character until eof character is read.
	// Output it to screen.
	while (!InStream.eof()) {
		//Read each character.
		InStream.get(ch);    

		// make sure we don't write any odd characters on screen
		if (!InStream.eof()) 
		{
			cout << ch;  //Write to screen
		}
	}

	InStream.close();
}

Running Session

mercury[243]% CC -LANG:std FileRead.cpp -o FileRead
mercury[244]% FileRead
This program reads the content of a file and prints it out on the screen.
Enter input file name: test.txt
Here is the content of test.txt:
It is easier to resist at the beginning than at the end.
                -- Leonardo da Vinci

mercury[245]% 


Last modified: Friday, 21-Aug-2020 15:28:14 CST
Copyright 2000 Department of Computer Science, University of Regina.

 CS Dept Home Page

Teaching Material

C++ Index Page