0% found this document useful (0 votes)
3 views

08 - Input and Output File

The document provides an overview of file input and output in computing, explaining how files can be used for data storage and retrieval in programs. It details the steps for file operations, including opening, using, and closing files, as well as the different data types used for file handling in C++. Examples are provided to illustrate file operations such as reading from and writing to files.

Uploaded by

Enoch
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

08 - Input and Output File

The document provides an overview of file input and output in computing, explaining how files can be used for data storage and retrieval in programs. It details the steps for file operations, including opening, using, and closing files, as well as the different data types used for file handling in C++. Examples are provided to illustrate file operations such as reading from and writing to files.

Uploaded by

Enoch
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

TOPIC 08

INPUT AND OUTPUT FILE


Fundamentals of Computing
(FSPK0022)
Introduction to Files
File Input and Output
 Can use files instead of keyboard and monitor screen for
program input and output.

 File: a set of data stored on a computer, often on a disk drive.


 Allows data to be retained between program runs.

 Programs can read from and/ or write to files.

 Used in many applications: word processing, databases,


spreadsheets, compilers.

 Steps: (1) Open the file (2) Use the file (read from, write to,
or both) (3) Close the file.
File Operations
 Requires fstream header file:
 use ifstream data type for input files.
 use ofstream data type for output files.
 use fstream data type for both input, output files.

 ifstream:
 Open for input only and file cannot be written to.
 Open fails if file does not exist.

 ofstream:
 Open for output only and file cannot be read from.
 File created if no file exists.
 File contents erased if file exists.
File Operations (cont.)

 fstream object can be used for either input or output.

 fstream: must specify mode on the open statement. Sample


modes:
 ios::in for input mode.
 ios::out for output mode.
 ios::binary for binary mode.
 ios::app for append mode. All output operations are performed at
the end of the file, appending the content to the current content of
the file.
Opening Files
 Create a link between file name (outside the program) and
file stream object (inside the program).

 Filename may include drive and/or path info.

 ifstream and ofstream - use the open() member function:


infile.open("inventory.dat");
outfile.open("report.txt");

 fstream - use the open() member function and mode(s):


infile.open("inventory.dat", ios::in);
outfile.open("report.txt", ios::out);
Opening Files (cont.)
 fstream - can be combined on open call:
dFile.open("class.txt", ios::in | ios::out);

 Can open file at declaration:


ifstream gradeList("grades.txt");
fstream infile("inventory.dat", ios::in);
fstream file("class.txt", ios::in | ios::out);

 Output file will be created if necessary; existing file will be


erased first.

 Input file must exist for open to work.


Opening Files (cont.)
 File stream object set to 0(false), if open failed. Example:
if (!input)
{ cout << “ERROR: Cannot open file\n”;
exit(1); }

 Can use fail() member function to detect file open error:


if (input.fail())
{ cout << “ERROR: Cannot open file\n”;
exit(1); }

 Can use is_open() member function to check if a file is open:


if (!input.is_open())
{ cout << “ERROR: Cannot open file\n”;
exit(1); }
Using Files
 Can use output file object and << to send data to a file:
outfile << "Inventory report";

 Can use input file object and >> to copy data from file to
variables:
infile >> partNum;
infile >> qtyInStock >> qtyOnOrder;

 Can use eof() member function to test for end of input file.
Closing Files
 Use the close() member function:
infile.close();
outfile.close();

 Don’t wait for operating system to close files at program end:


 may be limit on number of open files.
 may be buffered output data waiting to send to file.
Example 1: File Operations
#include <iostream> //copy 10 numbers between files
#include <fstream>
using namespace std;

int main()
{
fstream infile("input.txt", ios::in); // open the files
fstream outfile("output.txt", ios::out);
int num;

for (int i = 1; i <= 10; i++) {


infile >> num; // use the files
outfile << num; }

infile.close(); // close the files


outfile.close();
}
Example 2: File Operations
Example 3: File Operations
Example 4: File Operations
#include <fstream>
using namespace std;
int main()
{
ifstream input("inputfile.txt");
char str[80];

if (!input)
{
cout << "While opening a file an error is encountered" << endl;
return 0;
}
else
cout << "File is successfully opened" << endl;
while(!input.eof())
{
input.getline(str, 80);
cout << str << endl;
}
input.close();
return 0;
}
Example 5: File Operations
#include <iostream>
#include <fstream>
using namespace std;
int main ()
{
int num;
ifstream inp("input.txt"); // open the input file
ofstream out("output.txt"); // open the output file
if (!inp.is_open()) // check for successful opening
{
cout << "Input file could not be opened! Terminating!\n;
return 0;
}
while (inp >> num)
out << num * 2 << endl;

inp.close();
out.close();
cout << "Done!" << endl;
return 0;
}

You might also like