+ All Categories
Home > Documents > CSC 107 – Programming For Science. Today’s Goal Get familiar with opening & closing files ...

CSC 107 – Programming For Science. Today’s Goal Get familiar with opening & closing files ...

Date post: 02-Jan-2016
Category:
Upload: nathan-allen
View: 215 times
Download: 0 times
Share this document with a friend
Popular Tags:
28
LECTURE 25: OPENING & CLOSING A FILE CSC 107 – Programming For Science
Transcript
Page 1: CSC 107 – Programming For Science. Today’s Goal  Get familiar with opening & closing files  Declaring variables for use with files  Using variables.

LECTURE 25: OPENING & CLOSING A FILE

CSC 107 – Programming For Science

Page 2: CSC 107 – Programming For Science. Today’s Goal  Get familiar with opening & closing files  Declaring variables for use with files  Using variables.

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

Page 3: CSC 107 – Programming For Science. Today’s Goal  Get familiar with opening & closing files  Declaring variables for use with files  Using variables.

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

Page 4: CSC 107 – Programming For Science. Today’s Goal  Get familiar with opening & closing files  Declaring variables for use with files  Using variables.

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

Page 5: CSC 107 – Programming For Science. Today’s Goal  Get familiar with opening & closing files  Declaring variables for use with files  Using variables.

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

Page 6: CSC 107 – Programming For Science. Today’s Goal  Get familiar with opening & closing files  Declaring variables for use with files  Using variables.

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

Page 7: CSC 107 – Programming For Science. Today’s Goal  Get familiar with opening & closing files  Declaring variables for use with files  Using variables.

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

Page 8: CSC 107 – Programming For Science. Today’s Goal  Get familiar with opening & closing files  Declaring variables for use with files  Using variables.

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

Page 9: CSC 107 – Programming For Science. Today’s Goal  Get familiar with opening & closing files  Declaring variables for use with files  Using variables.

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

Page 10: CSC 107 – Programming For Science. Today’s Goal  Get familiar with opening & closing files  Declaring variables for use with files  Using variables.

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

Page 11: CSC 107 – Programming For Science. Today’s Goal  Get familiar with opening & closing files  Declaring variables for use with files  Using variables.

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

Page 12: CSC 107 – Programming For Science. Today’s Goal  Get familiar with opening & closing files  Declaring variables for use with files  Using variables.

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

Page 13: CSC 107 – Programming For Science. Today’s Goal  Get familiar with opening & closing files  Declaring variables for use with files  Using variables.

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;

Page 14: CSC 107 – Programming For Science. Today’s Goal  Get familiar with opening & closing files  Declaring variables for use with files  Using variables.

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;

Page 15: CSC 107 – Programming For Science. Today’s Goal  Get familiar with opening & closing files  Declaring variables for use with files  Using variables.

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;

Page 16: CSC 107 – Programming For Science. Today’s Goal  Get familiar with opening & closing files  Declaring variables for use with files  Using variables.

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

Page 17: CSC 107 – Programming For Science. Today’s Goal  Get familiar with opening & closing files  Declaring variables for use with files  Using variables.

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

Page 18: CSC 107 – Programming For Science. Today’s Goal  Get familiar with opening & closing files  Declaring variables for use with files  Using variables.

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);

Page 19: CSC 107 – Programming For Science. Today’s Goal  Get familiar with opening & closing files  Declaring variables for use with files  Using variables.

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

Page 20: CSC 107 – Programming For Science. Today’s Goal  Get familiar with opening & closing files  Declaring variables for use with files  Using variables.

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;

Page 21: CSC 107 – Programming For Science. Today’s Goal  Get familiar with opening & closing files  Declaring variables for use with files  Using variables.

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?

Page 22: CSC 107 – Programming For Science. Today’s Goal  Get familiar with opening & closing files  Declaring variables for use with files  Using variables.

What Happens To Existing File?

Page 23: CSC 107 – Programming For Science. Today’s Goal  Get familiar with opening & closing files  Declaring variables for use with files  Using variables.

Oops!

Page 24: CSC 107 – Programming For Science. Today’s Goal  Get familiar with opening & closing files  Declaring variables for use with files  Using variables.

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

Page 25: CSC 107 – Programming For Science. Today’s Goal  Get familiar with opening & closing files  Declaring variables for use with files  Using variables.

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

Page 26: CSC 107 – Programming For Science. Today’s Goal  Get familiar with opening & closing files  Declaring variables for use with files  Using variables.

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();

Page 27: CSC 107 – Programming For Science. Today’s Goal  Get familiar with opening & closing files  Declaring variables for use with files  Using variables.

Your Turn

Get into your groups and try this assignment

Page 28: CSC 107 – Programming For Science. Today’s Goal  Get familiar with opening & closing files  Declaring variables for use with files  Using variables.

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


Recommended