meljun cortes--it102 file handler c++ lecture 2nd
TRANSCRIPT
-
8/8/2019 MELJUN CORTES--IT102 FILE Handler C++ Lecture 2nd
1/27
FILE HANDLING IN
C++
-
8/8/2019 MELJUN CORTES--IT102 FILE Handler C++ Lecture 2nd
2/27
Files (Streams)
Files are used to store data in a relatively
permanent form, on floppy disk, hard disk,
tape or other form of secondary storage.Files can hold huge amounts of data if need
be. Ordinary variables (even records and
arrays) are kept in main memory which istemporary and rather limited in size. The
following is a comparison of the two types
of storage:
-
8/8/2019 MELJUN CORTES--IT102 FILE Handler C++ Lecture 2nd
3/27
Main memory
y Made up of RAM chips.
y Used to hold a programwhen it is running,
including the values of its
variables (whether
integer, char, an array,
etc.)
y Can only hold relatively
small amounts of data.
y Is temporary (as soon as
the program is done or
the power goes out all of
these values are gone).
y Gives fast access to the
data (all electronic).
Secondary memory
y Usually a disk drive (or
magnetic tape).y Used to hold files (where a file
can contain data, a program,
text, etc.)
y
Can hold rather large amountsof data.
y Is fairly permanent. (A file
remains even if the power goes
out. It will last until you erase it,
as long as the disk isn't
damaged, at least.)
Access to the data is
considerably slower (due to
moving parts).
-
8/8/2019 MELJUN CORTES--IT102 FILE Handler C++ Lecture 2nd
4/27
C++ STREAMS
A Stream is a general name given to flow ofdata.
Different streams are used to representdifferent kinds of data flow.
Each stream is associated with a particularclass, which contains member functions anddefinitions for dealing with that particularkind of data flow.
-
8/8/2019 MELJUN CORTES--IT102 FILE Handler C++ Lecture 2nd
5/27
Flow of Data.
PROGRAM
DEVICES OR
FILES
Input
Stream
>>
Output
Stream
-
8/8/2019 MELJUN CORTES--IT102 FILE Handler C++ Lecture 2nd
6/27
The following classes in C++
have access to file input andoutput functions:
ifstream
ofstream
fstream
-
8/8/2019 MELJUN CORTES--IT102 FILE Handler C++ Lecture 2nd
7/27
The Stream Class Hierarchy
ios
istream
get()
getline()
read()
>>
ostream
put()
write()
-
8/8/2019 MELJUN CORTES--IT102 FILE Handler C++ Lecture 2nd
8/27
DIFFERENT FILE OPERATIONS
OPENING A FILE
CLOSING A FILE
READING FROM A FILE
WRITING ON A FILE
CHECKING FOR END OF FILE
-
8/8/2019 MELJUN CORTES--IT102 FILE Handler C++ Lecture 2nd
9/27
OPENING A FILE
1. By using the CONSTRUCTOR of the
stream class.
ifstream transaction(sales.dly);
ofstream result(result.02);
2. By using the open() function of the stream
class
ifstream transaction;
transaction.open(sales.dly);
(Associating a stream with a file)
-
8/8/2019 MELJUN CORTES--IT102 FILE Handler C++ Lecture 2nd
10/27
FileMode Parameters
PARAMETER MEANING Ios::app Append to end-of file
Ios::ate goto end of file on opening
Ios::binary binary file
Ios::in Open existing file for reading
Ios::nocreate open fails if file doesnt exist
Ios::noreplace open fails if file already exists
Ios::out creates new file for writing on
Ios::trunc Deletes contents if it exists
The mode can combine two or more modes using bit wise
or ( | )
-
8/8/2019 MELJUN CORTES--IT102 FILE Handler C++ Lecture 2nd
11/27
Checking For Successful File Opening
ifstream transaction(sales.dly);
if (transcation == NULL)
{
cout
-
8/8/2019 MELJUN CORTES--IT102 FILE Handler C++ Lecture 2nd
12/27
Closing of File
Stream_name.close();
e.g., transaction.close();
-
8/8/2019 MELJUN CORTES--IT102 FILE Handler C++ Lecture 2nd
13/27
Types of Files
. The two basic types are
textand
binary.
A text file consists of readable characters
separated into lines by newline characters.
(On most PCs, the newline character isactually represented by the two-character
sequence of carriage return (ASCII 13), line
feed (ASCII 10).
-
8/8/2019 MELJUN CORTES--IT102 FILE Handler C++ Lecture 2nd
14/27
Abinary file stores data to disk in the same
form in which it is represented in main
memory.
If you ever try to edit a binary file containing
numbers you will see that the numbers appear
as nonsense characters. Not having totranslate numbers into a readable form makes
binary files somewhat more efficient.
Binary files also do not normally useanything to separate the data into lines. Such
a file is just a stream of data with nothing in
particular to separate components.
-
8/8/2019 MELJUN CORTES--IT102 FILE Handler C++ Lecture 2nd
15/27
When using a binary file we write whole
record data to the file at once.When using atext file, we write out separately each of the
pieces of data about a given record.
The text file will be readable by an editor,but the numbers in the binary file will not
be readable in this way.
The programs to create the data files will
differ in how they open the file and in how
they write to the file.
-
8/8/2019 MELJUN CORTES--IT102 FILE Handler C++ Lecture 2nd
16/27
For the binary file we will use write to
write to the file, whereas for the text file wewill use the usual output operator()
-
8/8/2019 MELJUN CORTES--IT102 FILE Handler C++ Lecture 2nd
17/27
EXAMPLES
Creation of a text file
-
8/8/2019 MELJUN CORTES--IT102 FILE Handler C++ Lecture 2nd
18/27
:
Sequential access. With this type of file
access one must read the data in order,
much like with a tape, whether the datais really stored on tape or not.
Random access (or direct access). This
type of file access lets you jump to any
location in the file, then to any other,
etc., all in a reasonable amount of time.
Types of File Access
-
8/8/2019 MELJUN CORTES--IT102 FILE Handler C++ Lecture 2nd
19/27
FILE POINTERS
-
8/8/2019 MELJUN CORTES--IT102 FILE Handler C++ Lecture 2nd
20/27
FILE POINTERS
Each file object has two integer values
associated with it :
get pointer
put pointer
These values specify the byte number in the
file where reading or writing will takeplace.
-
8/8/2019 MELJUN CORTES--IT102 FILE Handler C++ Lecture 2nd
21/27
File pointers..
By default reading pointer is set at the
beginning and writing pointer is set at the
end (when you open file in ios::app mode)
There are times when you must take control
of the file pointers yourself so that you canread from and write to an arbitrary location
in the file.
-
8/8/2019 MELJUN CORTES--IT102 FILE Handler C++ Lecture 2nd
22/27
Functions associated with file
pointers :
The seekg() and tellg() functions allow you
to set and examine the get pointer.
The seekp() and tellp() functions allow you
to set and examine theput pointer.
-
8/8/2019 MELJUN CORTES--IT102 FILE Handler C++ Lecture 2nd
23/27
seekg() function :
With one argument :
seekg(k) where k is absolute position from
the beginning. The start of the file is byte 0
Begin FileEnd
k bytes ^
File pointer
The seekg() function with one argument
-
8/8/2019 MELJUN CORTES--IT102 FILE Handler C++ Lecture 2nd
24/27
seekg() function :
With two arguments :the first argument represents an offset from a particular
location in the file.
the second specifies the location from which the offset is
measured.
Begin End
^Offset from Begin
The seekg() function with two argument
-
8/8/2019 MELJUN CORTES--IT102 FILE Handler C++ Lecture 2nd
25/27
seekg() function :
With two arguments :
Begin End
^Offset from Begin
The seekg() function with two argument
^
^
Offset from end
Offset from current
position
-
8/8/2019 MELJUN CORTES--IT102 FILE Handler C++ Lecture 2nd
26/27
//
#include
#include #include
void main()
{
//clrscr();
char c,d,ans;
char str[80];
ofstream outfl("try.txt"),out("cod.dat");
ifstream infl;
do
{ cout
-
8/8/2019 MELJUN CORTES--IT102 FILE Handler C++ Lecture 2nd
27/27