Save
study foc final
chapter 8 foc
Save
Share
Learn
Content
Leaderboard
Learn
Created by
Amna Herman
Visit profile
Cards (13)
File
A set of
data
stored on a computer, often on a
disk drive
File input and output
Can use files instead of keyboard and monitor screen for program
input
and
output
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
File operations
1.
Open
the file
2.
Use
the file (read from, write to, or both)
3.
Close
the file
ifstream
Data type for
input files
, open for
input
only and file cannot be written to, open fails if file does not exist
ofstream
Data type for output files, open for
output
only and file cannot be read from, file created if no file exists, file
contents erased
if file exists
fstream
Data type for both
input
and output files, must specify mode on the
open
statement
fstream open modes
ios
::in for
input
mode
ios
::out for
output mode
ios::
binary
for
binary mode
ios
::app for
append mode
Opening files
1. Create a
link
between file name (
outside
the program) and file stream object (inside the program)
2. Filename may include
drive
and/or
path info
3. Use the
open
()
member
function
Output file will be created if necessary; existing file will be
erased
first
Input file must exist for
open
to work
Checking file open errors
1. File stream object set to 0(false), if open failed
2. Use
fail
() member function to detect file open error
3. Use is_
open
() member function to check if a file is
open
Using files
1. Use output file object and << to
send
data to a file
2. Use
input file
object and >> to copy data from file to
variables
3. Use eof() member function to test for end of input file
Closing files
1. Use the
close
() member function
2. Don't wait for
operating
system to
close
files at program end