Technology Advances
Formatted I/O, File I/O
1. Formatted I/O using ios member functions
Code Listing:
// 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:
$$$$$$$$$$$+1.00e+00
$$$$$$$$$$$-1.23e+00
$$$$$$$$$$$+2.35e+03
$$$$$$$$$$$+2.34e+01
$$$$$$$$$$$+4.53e+01
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:
$$$$$$$$$$$+1.00e+00
$$$$$$$$$$$-1.23e+00
$$$$$$$$$$$+2.35e+03
$$$$$$$$$$$+2.34e+01
$$$$$$$$$$$+4.53e+01
3. Define your own I/O manipulators
Code listing:
// 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:
Enter an integer: 19
Hexdecimal is: 13
4. File I/O: Writing to a text file
Code Lising
// 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
Please enter output file name: out.txt
Content written to out.txt.
5. File I/O: Reading from a text file
Code Lising
// 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
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
Contact
Microlaunch
Rayat Institute of Engineering and Information Technology, Railmajra, Near Ropar (Chandigarh)
+91-9463349819
microlaunch@rediffmail.com
Rayat Institute of Engineering and Information Technology, Railmajra, Near Ropar (Chandigarh)
+91-9463349819
microlaunch@rediffmail.com