Date post: | 02-Jan-2016 |
Category: |
Documents |
Upload: | nathan-allen |
View: | 215 times |
Download: | 0 times |
LECTURE 25: OPENING & CLOSING A FILE
CSC 107 – Programming For Science
Today’s Goal
Get familiar with opening & closing files Declaring variables for use with files Using variables to open files for reading &
writing Closing files and understand why you
should do so
Image To Sharpen
I have a (fuzzy) 1024 x 768 picture to sharpen Only 786,432 numbers to type in to
yesterday's lab Will also need to click each pixel to update
with result
More Data Entry Positions
Testing improved jet designs for oeingB-ay Using program to simulate designs' lift &
drag 5 possible designs (each 150MB) to test
this iteration Once results available, will tweak & retest
designs Need room of touch typists for all this data
entry
This Is (Semi-Real) Problem
Large hadron collider about to come on-line No black hole when smashing particles at
high speeds Creates 28.5 GB/min for nerds seeking
truth & beauty
This Is (Semi-Real) Problem
Large hadron collider about to come on-line No black hole when smashing particles at
high speeds Creates 28.5 GB/min for nerds seeking
truth & beauty Hired trained monkeys to type data into
programs
This Is (Semi-Real) Problem
Large hadron collider about to come on-line No black hole when smashing particles at
high speeds Creates 28.5 GB/min for nerds seeking
truth & beauty Hired trained monkeys to type data into
programs college students
This Is (Semi-Real) Problem
Large hadron collider about to come on-line No black hole when smashing particles at
high speeds Creates 28.5 GB/min for nerds seeking
truth & beauty Hired trained monkeys to type data into
programs college students
Yeah, Right
Real world demands we use files for most I/O
Data files used to start and/or end most projects May contain: game levels, analysis results,
CCD pics Way to read & write files needed to be
useful
Program Basics For Files
All built-in file I/O code means adding to header
#include <fstream> Place with other #includes to use files in
program Even if no files are used, no cost to adding
this line Must specify namespace of file I/O
code, also If you really want, this can be done
individual but…
using namespace std; much easier and probably habit by now
anyway
Opening a File
To use file, we need variable to use in program Numbers, cStrings, and booleans mixed in
the file Previous type (& arrays) do not make sense
to use C++ provides new types to refer to file
itself
Opening a File
Within program, may use file in 2 possible ways To read file, ifstream variables will be
needed Need variable of type ofstream to write to
file
File Variable Declaration
Types are new, but still just declaring variables Requires type & name of variable being
declared Names for human use; normal scoping
rules apply Can pass as argument if parameter of same
type Cannot use in equations: files are NOT
numbersofstream fout;ifstream fin;ofstream bob, bill, babs, barb;ifstream thisIsAFileVariable;ofstream monkey = "foo.txt";ifstream fourFile = 4;
File Variable Declaration
Types are new, but still just declaring variables Requires type & name of variable being
declared Names for human use; normal scoping
rules apply Can pass as argument if parameter of same
type Cannot use in equations: files are NOT
numbersofstream fout;ifstream fin;ofstream bob, bill, babs, barb;ifstream thisIsAFileVariable;ofstream monkey = "foo.txt";ifstream fourFile = 4;
File Variable Declaration
Types are new, but still just declaring variables Requires type & name of variable being
declared Names for human use; normal scoping
rules apply Can pass as argument if parameter of same
type Cannot use in equations: files are NOT
numbersofstream fout;ifstream fin;ofstream bob, bill, babs, barb;ifstream thisIsAFileVariable;ofstream monkey = "foo.txt";ifstream fourFile = 4;
Name That File
Two ways to open a file once we have the name No matter which used, must know name of
file Defaults to current directory, if only a name
specified By including in name, can use other
directories No standard way to do this – depends on
the OS
Name That File
Two ways to open a file once we have the name No matter which used, must know name of
file Defaults to current directory, if only a name
specified By including in name, can use other
directories No standard way to do this – depends on
the OS
Opening the File
Can open file when variable declaredchar nameLoc[] = "bobbobbob";char sndName[] = "csi.txt";ifstream fin("image.dat");ofstream fout(nameLoc);ifstream bobism(sndName);ofstream cookies("monster.txt");
Even after declaration, files can be openedifstream because;ofstream isaidso;because.open("mightMakes.right");cin >> sndName;isaidso.open(sndName);
Did I Do Good?
May not always be successful opening a file Cannot open and read file that does not
exist May not have permission to access a
certain file Cannot do impossible & write to
nonexistent drive Use built-in is_open function to check
this Called in manner similar to cout functions
like setf If variable attached to open file, function
returns true Returns false if variable not used to open
file, yet If attempt to open file fails, will also return
false
Examples of is_open
char sndName[];ifstream because, foo("foo.txt");ofstream isaidso, bar("snafu");cout << because.is_open() << endl;cout << foo.is_open() << endl;cout << bar.is_open() << endl;because.open("mightMakes.right");cin >> sndName;isaidso.open(sndName);cout << because.is_open() << endl;cout << isaidso.is_open() << endl;
Upon Opening The Location Is… Once open, read & write from start of
file As logical a choice as any other location to
start at If reading, can start getting all data from
file When writing to existing file what will
happen?
What Happens To Existing File?
Oops!
Opening a File
Within program, may use file in 2 possible ways To read file, ifstream variables will be
needed Need variable of type ofstream to write to
file
Opening a File
Within program, may use file in 2 possible ways To read file, ifstream variables will be
needed Need variable of type ofstream to write to
file Open ofstream 2 different ways depending
on use
ofstream nukeIt("byebye.txt");ofstream begone;begone.open("erasedOld.dat");
ofstream keepIt("saved", ios::app);ofstream faithAlone;faithAlone.open("preserve", ios::app);
3
Closing File
Important to close files once you are done Program will delay saving data to make it
faster Crashes cause data loss if saves had been
waiting Until file is closed may be locked from other
uses Immediately saved on close, so insures
data is safe Can open more files if limit of open files
reachedofstream phillies("nlChamp");ifstream yankees("evilEmpire");phillies.close();yankees.close();
Your Turn
Get into your groups and try this assignment
For Next Lecture
Actually using files in Section 11.6 – 11.7 What can we do with open files? Where have we seen these commands
before? What does this mean & how are they
related? Angel also has Weekly Assignment #8
due Tues.
Midterm #2 in class next Friday