|
|
|
|
|
Reading and writing text file |
|
Declare file stream variable |
|
Open a file by indicating its name |
|
Get data from a file |
|
Write data into a file |
|
Close a file |
|
|
|
|
All of the programs we have written until now
have read their input from the keyboard and displayed their output on the
screen. |
|
Start from this week, we will use Files as the
input source and output destination |
|
|
|
|
|
To store and retrieve data on a file in C++,
three items are required: |
|
A file |
|
A file stream |
|
A mode |
|
A file is a collection of data that is stored
under a common name, usually on a disk, magnetic tape, or CD-ROM. |
|
A file stream is any stream that connects a file
stored on a physical device. |
|
A stream is a one-way transmission path between
a source and a destination. |
|
|
|
|
|
|
|
Stream objects are created from stream classes.
We have already met two stream objects in this course: |
|
Input stream object (istream): cin |
|
Output stream object (ostream): cout |
|
cin provides a transmission path from keyboard
to a program |
|
cout provides a transmission path from program
to terminal screen. |
|
cin and cout are defined in iostream.h |
|
#include <iostream.h> |
|
|
|
|
|
|
|
File streams connect program to a file rather
than the keyboard or terminal screen. |
|
File streams objects must be explicitly
declared. |
|
File streams are defined in fstream.h |
|
include <fstream.h> |
|
|
|
|
|
|
|
|
|
To access a disk file, you need to open a file
variable. |
|
File variables are variables of type : |
|
ifstream (for input), |
|
ofstream (for output), |
|
or fstream (for both input and output). |
|
You will be using similar functions that are in
filetools.h in your labs but I want you to see how it is done in C++. |
|
|
|
|
|
|
|
|
|
Open a file by indicating the file name |
|
If the file is in the same directory as the
program (current directory) |
|
MyInFile.open(“MyInData.txt”) |
|
MyOutFile.open(“MyOutData.txt”); |
|
If the file is in different directory, should
put the full path |
|
MyInFile.open(“e:\\com1100\\data\\MyInData.txt”); |
|
MyOutFile.open(“e:\\com1100\\data\\MyOutData.txt”); |
|
|
|
|
|
|
|
|
Open a file by indicating the file name |
|
If define file name as character array |
|
const int MAXLENGTH = 21; |
|
char fileName[MAXLENGTH]; |
|
cout << “Please enter a file name: “; |
|
cin >> fileName; |
|
MyInFile.open(filename); |
|
|
|
|
|
|
|
|
Open a file by indicating the file name |
|
If define file name as string |
|
string inFileName; |
|
RequestString("Enter data file
name", inFileName); |
|
ifstream MyInFile; |
|
MyInFile.open(inFileName.c_str()); |
|
string outFileName; |
|
RequestString("Enter out file
name", outFileName); |
|
ofstream myOutFile; |
|
myOutFile.open(outFileName.c_str()); |
|
Here, function inFileName.c_str() converts a
string to a character array |
|
|
|
|
|
|
|
|
|
Check if file has been successfully opened |
|
If ( !MyInFile ) { |
|
cout << “ Can not open the file !”
<< endl; |
|
exit (1); |
|
} |
|
Note: |
|
exit( ) function is a request to the operating
system to end program execution immediately. |
|
exit( ) function requires inclusion of the stdlib.h
header file |
|
exit( )’s single integer argument is passed
directly to the operating system program action or user inspection. |
|
|
|
|
|
|
Get data from the file |
|
The simplest way works just like cin with >> |
|
MyInFile >> x >> y >> radius
>> R >> G >> B; |
|
|
|
You also can read a single character form a File
with get method |
|
char ch; |
|
MyInFile.get(ch); |
|
|
|
|
|
|
|
|
Write data into the file |
|
The simplest way works just like cout with << |
|
MyOutFile << x << y << radius
<< R << G << B; |
|
|
|
You also can write a single character into a
File with put method |
|
char ch; |
|
MyOutFile.put(ch); |
|
|
|
|
|
|
|
Close the file |
|
MyInFile.close() |
|
MyOutFile.close(); |
|
|
|