csc 107 – programming for science. today’s goal get familiar with opening & closing files ...
DESCRIPTION
Image To Sharpen I have a (fuzzy) 1024 x 768 picture to sharpen 786,432 Only 786,432 numbers to type in to yesterday's lab Will also need to click each pixel to update with resultTRANSCRIPT
![Page 1: CSC 107 – Programming For Science. Today’s Goal Get familiar with opening & closing files Declaring variables for use with files Using variables](https://reader033.vdocuments.net/reader033/viewer/2022052706/5a4d1b1f7f8b9ab059994c4f/html5/thumbnails/1.jpg)
LECTURE 33: USING FILE I/O
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](https://reader033.vdocuments.net/reader033/viewer/2022052706/5a4d1b1f7f8b9ab059994c4f/html5/thumbnails/2.jpg)
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 Understand what it means: ALL I/O is
file I/O Common bugs to avoid when coding with
files in C++ Get a better understanding of what >> & <<
do cin, cout are variables: know what their
types are
![Page 3: CSC 107 – Programming For Science. Today’s Goal Get familiar with opening & closing files Declaring variables for use with files Using variables](https://reader033.vdocuments.net/reader033/viewer/2022052706/5a4d1b1f7f8b9ab059994c4f/html5/thumbnails/3.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022052706/5a4d1b1f7f8b9ab059994c4f/html5/thumbnails/4.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022052706/5a4d1b1f7f8b9ab059994c4f/html5/thumbnails/5.jpg)
This Is (Semi-Real) Problem
Large hadron collider eventually work as designed 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](https://reader033.vdocuments.net/reader033/viewer/2022052706/5a4d1b1f7f8b9ab059994c4f/html5/thumbnails/6.jpg)
This Is (Semi-Real) Problem
Large hadron collider eventually work as designed No black hole when smashing particles at
high speeds Creates 28.5 GB/min for nerds seeking
truth & beauty Hired trained monkeys to type all
244,813,135,872 bits
![Page 7: CSC 107 – Programming For Science. Today’s Goal Get familiar with opening & closing files Declaring variables for use with files Using variables](https://reader033.vdocuments.net/reader033/viewer/2022052706/5a4d1b1f7f8b9ab059994c4f/html5/thumbnails/7.jpg)
This Is (Semi-Real) Problem
Large hadron collider eventually work as designed No black hole when smashing particles at
high speeds Creates 28.5 GB/min for nerds seeking truth
& beauty Hired trained monkeys to type all
244,813,135,872 bits 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](https://reader033.vdocuments.net/reader033/viewer/2022052706/5a4d1b1f7f8b9ab059994c4f/html5/thumbnails/8.jpg)
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 9: CSC 107 – Programming For Science. Today’s Goal Get familiar with opening & closing files Declaring variables for use with files Using variables](https://reader033.vdocuments.net/reader033/viewer/2022052706/5a4d1b1f7f8b9ab059994c4f/html5/thumbnails/9.jpg)
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 do this individually but…
using namespace std;…much easier and probably habit by now anyway
![Page 10: CSC 107 – Programming For Science. Today’s Goal Get familiar with opening & closing files Declaring variables for use with files Using variables](https://reader033.vdocuments.net/reader033/viewer/2022052706/5a4d1b1f7f8b9ab059994c4f/html5/thumbnails/10.jpg)
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 11: CSC 107 – Programming For Science. Today’s Goal Get familiar with opening & closing files Declaring variables for use with files Using variables](https://reader033.vdocuments.net/reader033/viewer/2022052706/5a4d1b1f7f8b9ab059994c4f/html5/thumbnails/11.jpg)
Types Used For Files
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 12: CSC 107 – Programming For Science. Today’s Goal Get familiar with opening & closing files Declaring variables for use with files Using variables](https://reader033.vdocuments.net/reader033/viewer/2022052706/5a4d1b1f7f8b9ab059994c4f/html5/thumbnails/12.jpg)
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 Cannot assign variables but can use as
parameters 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 13: CSC 107 – Programming For Science. Today’s Goal Get familiar with opening & closing files Declaring variables for use with files Using variables](https://reader033.vdocuments.net/reader033/viewer/2022052706/5a4d1b1f7f8b9ab059994c4f/html5/thumbnails/13.jpg)
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 Cannot assign variables but can use as
parameters 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](https://reader033.vdocuments.net/reader033/viewer/2022052706/5a4d1b1f7f8b9ab059994c4f/html5/thumbnails/14.jpg)
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 Cannot assign variables but can use as
parameters 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](https://reader033.vdocuments.net/reader033/viewer/2022052706/5a4d1b1f7f8b9ab059994c4f/html5/thumbnails/15.jpg)
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 16: CSC 107 – Programming For Science. Today’s Goal Get familiar with opening & closing files Declaring variables for use with files Using variables](https://reader033.vdocuments.net/reader033/viewer/2022052706/5a4d1b1f7f8b9ab059994c4f/html5/thumbnails/16.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022052706/5a4d1b1f7f8b9ab059994c4f/html5/thumbnails/17.jpg)
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(“nomnomnom.txt");
Even after declaration, files can be openedifstream because;ofstream isaidso;because.open("mightMakes.right");cin >> sndName;isaidso.open(sndName);
![Page 18: CSC 107 – Programming For Science. Today’s Goal Get familiar with opening & closing files Declaring variables for use with files Using variables](https://reader033.vdocuments.net/reader033/viewer/2022052706/5a4d1b1f7f8b9ab059994c4f/html5/thumbnails/18.jpg)
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 19: CSC 107 – Programming For Science. Today’s Goal Get familiar with opening & closing files Declaring variables for use with files Using variables](https://reader033.vdocuments.net/reader033/viewer/2022052706/5a4d1b1f7f8b9ab059994c4f/html5/thumbnails/19.jpg)
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 20: CSC 107 – Programming For Science. Today’s Goal Get familiar with opening & closing files Declaring variables for use with files Using variables](https://reader033.vdocuments.net/reader033/viewer/2022052706/5a4d1b1f7f8b9ab059994c4f/html5/thumbnails/20.jpg)
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 21: CSC 107 – Programming For Science. Today’s Goal Get familiar with opening & closing files Declaring variables for use with files Using variables](https://reader033.vdocuments.net/reader033/viewer/2022052706/5a4d1b1f7f8b9ab059994c4f/html5/thumbnails/21.jpg)
Oops!
![Page 22: CSC 107 – Programming For Science. Today’s Goal Get familiar with opening & closing files Declaring variables for use with files Using variables](https://reader033.vdocuments.net/reader033/viewer/2022052706/5a4d1b1f7f8b9ab059994c4f/html5/thumbnails/22.jpg)
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 23: CSC 107 – Programming For Science. Today’s Goal Get familiar with opening & closing files Declaring variables for use with files Using variables](https://reader033.vdocuments.net/reader033/viewer/2022052706/5a4d1b1f7f8b9ab059994c4f/html5/thumbnails/23.jpg)
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 useofstream nukeIt("byebye.txt");ofstream begone;begone.open("erasedOld.dat");
ofstream keepIt("saved", ios::app);ofstream faithAlone;faithAlone.open("preserve", ios::app);
3
![Page 24: CSC 107 – Programming For Science. Today’s Goal Get familiar with opening & closing files Declaring variables for use with files Using variables](https://reader033.vdocuments.net/reader033/viewer/2022052706/5a4d1b1f7f8b9ab059994c4f/html5/thumbnails/24.jpg)
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 giants("nlChamp");ifstream yankees("evilEmpire");phillies.close();yankees.close();
![Page 25: CSC 107 – Programming For Science. Today’s Goal Get familiar with opening & closing files Declaring variables for use with files Using variables](https://reader033.vdocuments.net/reader033/viewer/2022052706/5a4d1b1f7f8b9ab059994c4f/html5/thumbnails/25.jpg)
Today's Key Point
Because of its history, all C++ I/O is file based Obvious when file is source of data or
target of write But also true when reading from keyboard Writing to screen also considered file I/O
![Page 26: CSC 107 – Programming For Science. Today’s Goal Get familiar with opening & closing files Declaring variables for use with files Using variables](https://reader033.vdocuments.net/reader033/viewer/2022052706/5a4d1b1f7f8b9ab059994c4f/html5/thumbnails/26.jpg)
Today's Key Point
You've been coding with files
since day 1
![Page 27: CSC 107 – Programming For Science. Today’s Goal Get familiar with opening & closing files Declaring variables for use with files Using variables](https://reader033.vdocuments.net/reader033/viewer/2022052706/5a4d1b1f7f8b9ab059994c4f/html5/thumbnails/27.jpg)
What Are cin & cout?
Statement needed for most file I/O#include <fstream>
To use cin & cout we must have statement:
#include <iostream>
![Page 28: CSC 107 – Programming For Science. Today’s Goal Get familiar with opening & closing files Declaring variables for use with files Using variables](https://reader033.vdocuments.net/reader033/viewer/2022052706/5a4d1b1f7f8b9ab059994c4f/html5/thumbnails/28.jpg)
What Are cin & cout?
Statement needed for most file I/O#include <fstream>
To use cin & cout we must have statement:
#include <iostream>
There is a method: similarity not an accident cin & cout are file variables defined by
system
![Page 29: CSC 107 – Programming For Science. Today’s Goal Get familiar with opening & closing files Declaring variables for use with files Using variables](https://reader033.vdocuments.net/reader033/viewer/2022052706/5a4d1b1f7f8b9ab059994c4f/html5/thumbnails/29.jpg)
What Are cin & cout?
Statement needed for most file I/O#include <fstream>
To use cin & cout we must have statement:
#include <iostream>
There is a method: similarity not an accident cin & cout are file variables defined by
system
![Page 30: CSC 107 – Programming For Science. Today’s Goal Get familiar with opening & closing files Declaring variables for use with files Using variables](https://reader033.vdocuments.net/reader033/viewer/2022052706/5a4d1b1f7f8b9ab059994c4f/html5/thumbnails/30.jpg)
Deep in Bowels of iostream
In iostream find 2 lines to be included in code:ifstream cin( );
ofstream cout( );
Already written code reading from a file Use ifstream like cin to read ASCII text in
a file Also know how to write ASCII text to a
file As with cout, ofstreams writes text to a
file
![Page 31: CSC 107 – Programming For Science. Today’s Goal Get familiar with opening & closing files Declaring variables for use with files Using variables](https://reader033.vdocuments.net/reader033/viewer/2022052706/5a4d1b1f7f8b9ab059994c4f/html5/thumbnails/31.jpg)
Read User's Typing With cin
Used to read one or more values at once:
cin >> variable;cin >> variable1 >> variable2;
Starts where last read stopped reading input
Automatically skips past whitespace Data type of variable determines what
is read Stops at first non-usable value found in the
input If input is not usable, will set variable equal
to 0
![Page 32: CSC 107 – Programming For Science. Today’s Goal Get familiar with opening & closing files Declaring variables for use with files Using variables](https://reader033.vdocuments.net/reader033/viewer/2022052706/5a4d1b1f7f8b9ab059994c4f/html5/thumbnails/32.jpg)
Read File W/ifstream Variable Used to read one or more values at
once:ifstream myFile;myFile >> variable;
myFile >> variable1 >> variable2; Starts where last read stopped reading
input Automatically skips past whitespace
Data type of variable determines what is read Stops at first non-usable value found in the
input If input is not usable, will set variable equal
to 0
![Page 33: CSC 107 – Programming For Science. Today’s Goal Get familiar with opening & closing files Declaring variables for use with files Using variables](https://reader033.vdocuments.net/reader033/viewer/2022052706/5a4d1b1f7f8b9ab059994c4f/html5/thumbnails/33.jpg)
Print to Screen Using cout
Easy to output: print text using cout
cout << “Hello World” << endl; Prints out whatever is placed between
quotes Value of variable printed if variable not in
quotes Use escape sequences for fancier text
output\n newline (move to start of next line)\t tab (go to next column that is multiple of 8)
Output using #include <iomanip> fancier
![Page 34: CSC 107 – Programming For Science. Today’s Goal Get familiar with opening & closing files Declaring variables for use with files Using variables](https://reader033.vdocuments.net/reader033/viewer/2022052706/5a4d1b1f7f8b9ab059994c4f/html5/thumbnails/34.jpg)
Print to File With ostream
Easy to output: output via ostream variable
ofstream outFile;outFile << “Hello World” << endl; Prints out whatever is placed between
quotes Value of variable printed if variable not in
quotes Use escape sequences for fancier text
output\n newline (move to start of next line)\t tab (go to next column that is multiple of 8)
Output using #include <iomanip> fancier
![Page 35: CSC 107 – Programming For Science. Today’s Goal Get familiar with opening & closing files Declaring variables for use with files Using variables](https://reader033.vdocuments.net/reader033/viewer/2022052706/5a4d1b1f7f8b9ab059994c4f/html5/thumbnails/35.jpg)
See How Easy It Is?
#include <iostream>#include <fstream>using namespace std;int main(void) {
int sum = 0; int val; cout << "-1 to quit or sum nums typed" << endl; cin >> val; while (val != -1) { sum += val; cout << val << endl; cin >> val; } return 0;}
![Page 36: CSC 107 – Programming For Science. Today’s Goal Get familiar with opening & closing files Declaring variables for use with files Using variables](https://reader033.vdocuments.net/reader033/viewer/2022052706/5a4d1b1f7f8b9ab059994c4f/html5/thumbnails/36.jpg)
See How Easy It Is?
#include <iostream>#include <fstream>using namespace std;int main(void) { ifstream fin; ofstream fout; int sum = 0; int val; fout << "-1 to quit or sum nums typed" << endl; fin >> val; while (val != -1) { sum += val; fout << val << endl; fin >> val; } return 0;}
![Page 37: CSC 107 – Programming For Science. Today’s Goal Get familiar with opening & closing files Declaring variables for use with files Using variables](https://reader033.vdocuments.net/reader033/viewer/2022052706/5a4d1b1f7f8b9ab059994c4f/html5/thumbnails/37.jpg)
And When We Run It?
#include <iostream>#include <fstream>using namespace std;int main(void) { ifstream fin; ofstream fout; int sum = 0; int val; fout << "-1 to quit or sum nums typed" << endl; fin >> val; while (val != -1) { sum += val; fout << val << endl; fin >> val; } return 0;}
![Page 38: CSC 107 – Programming For Science. Today’s Goal Get familiar with opening & closing files Declaring variables for use with files Using variables](https://reader033.vdocuments.net/reader033/viewer/2022052706/5a4d1b1f7f8b9ab059994c4f/html5/thumbnails/38.jpg)
And When We Run It?
![Page 39: CSC 107 – Programming For Science. Today’s Goal Get familiar with opening & closing files Declaring variables for use with files Using variables](https://reader033.vdocuments.net/reader033/viewer/2022052706/5a4d1b1f7f8b9ab059994c4f/html5/thumbnails/39.jpg)
Must Open File Before Using
How file opened unimportant, just that is open Open when declared, by giving file name as
cStringifstream bobIn("file.txt");ofstream whyNot(stringFromUser);
Open open later in program using open() routinebobIn.open(nameOfDataFile);whyNot.open("averagesWeCompute");
Variable must refer to open file or else it crashes Often add check with is_open() to protect
from crashs
![Page 40: CSC 107 – Programming For Science. Today’s Goal Get familiar with opening & closing files Declaring variables for use with files Using variables](https://reader033.vdocuments.net/reader033/viewer/2022052706/5a4d1b1f7f8b9ab059994c4f/html5/thumbnails/40.jpg)
See How Easy It Is?
#include <iostream>#include <fstream>using namespace std;int main(void) {
int sum = 0; int val; cout << "-1 to quit or sum nums typed" << endl; cin >> val; while (val != -1) { sum += val; cout << "Sum: " << val << endl; cin >> val; } return 0;}
![Page 41: CSC 107 – Programming For Science. Today’s Goal Get familiar with opening & closing files Declaring variables for use with files Using variables](https://reader033.vdocuments.net/reader033/viewer/2022052706/5a4d1b1f7f8b9ab059994c4f/html5/thumbnails/41.jpg)
See How Easy It Is?
#include <iostream>#include <fstream>using namespace std;int main() {
int sum = 0; int val; cout << "-1 to quit or sum nums typed" << endl; cin >> val; while (val != -1) { sum += val; cout << "Sum: " << val << endl; cin >> val; }
return 0;}
![Page 42: CSC 107 – Programming For Science. Today’s Goal Get familiar with opening & closing files Declaring variables for use with files Using variables](https://reader033.vdocuments.net/reader033/viewer/2022052706/5a4d1b1f7f8b9ab059994c4f/html5/thumbnails/42.jpg)
See How Easy It Is?
#include <iostream>#include <fstream>using namespace std;int main() { ifstream myFile("numbers.txt"); ofstream yNot("sums.out"); if (myFile.is_open() && yNot.is_open()) { int sum = 0; int val; yNot << "-1 to quit or sum nums typed" << endl; myFile >> val; while (val != -1) { sum += val; yNot << "Sum: " << val << endl; myFile >> val; } } return 0;}
![Page 43: CSC 107 – Programming For Science. Today’s Goal Get familiar with opening & closing files Declaring variables for use with files Using variables](https://reader033.vdocuments.net/reader033/viewer/2022052706/5a4d1b1f7f8b9ab059994c4f/html5/thumbnails/43.jpg)
See How Easy It Is?
#include <iostream>#include <fstream>using namespace std;int main() { ifstream myFile("numbers.txt"); ofstream yNot("sums.out"); if (myFile.is_open() && yNot.is_open()) { int sum = 0; int val; cout << "-1 to quit or sum nums typed" << endl; myFile >> val; while (val != -1) { sum += val; yNot << "Sum: " << val << endl; myFile >> val; } } return 0;}
![Page 44: CSC 107 – Programming For Science. Today’s Goal Get familiar with opening & closing files Declaring variables for use with files Using variables](https://reader033.vdocuments.net/reader033/viewer/2022052706/5a4d1b1f7f8b9ab059994c4f/html5/thumbnails/44.jpg)
Variables are Variables
Like all variables, can use files as parameters Argument must also be file variable for it to
compile Reading & writing continues through the
function Can only go forward in file no matter
what Within the function, "file position marker"
continues Cannot unring bell, does not overwrite file
on return As with cin to read, will not reread after
function
![Page 45: CSC 107 – Programming For Science. Today’s Goal Get familiar with opening & closing files Declaring variables for use with files Using variables](https://reader033.vdocuments.net/reader033/viewer/2022052706/5a4d1b1f7f8b9ab059994c4f/html5/thumbnails/45.jpg)
But…
Like all variables, can use files as parameters Argument must also be file variable for it to
compile Reading & writing continues through the
function Can only go forward in file no matter
what Within the function, "file position marker"
continues Cannot unring bell, does not overwrite file
on return As with cin to read, will not reread after
function
![Page 46: CSC 107 – Programming For Science. Today’s Goal Get familiar with opening & closing files Declaring variables for use with files Using variables](https://reader033.vdocuments.net/reader033/viewer/2022052706/5a4d1b1f7f8b9ab059994c4f/html5/thumbnails/46.jpg)
But…
Like all variables, can use files as parameters Argument must also be file variable for it to
compile Reading & writing continues through the
function Can only go forward in file no matter
what Within the function, "file position marker"
continues Cannot unring bell, does not overwrite file
on return As with cin to read, will not reread after
function
![Page 47: CSC 107 – Programming For Science. Today’s Goal Get familiar with opening & closing files Declaring variables for use with files Using variables](https://reader033.vdocuments.net/reader033/viewer/2022052706/5a4d1b1f7f8b9ab059994c4f/html5/thumbnails/47.jpg)
Butt…
Like all variables, can use files as parameters Argument must also be file variable for it to
compile Reading & writing continues through the
function Can only go forward in file no matter
what Within the function, "file position marker"
continues Cannot unring bell, does not overwrite file
on return As with cin to read, will not reread after
function
![Page 48: CSC 107 – Programming For Science. Today’s Goal Get familiar with opening & closing files Declaring variables for use with files Using variables](https://reader033.vdocuments.net/reader033/viewer/2022052706/5a4d1b1f7f8b9ab059994c4f/html5/thumbnails/48.jpg)
Variables are Variables
Like all variables, can use files as parameters Argument must also be file variable for it to
compile Reading & writing continues through the
function Can only go forward in file no matter
what Within the function, "file position marker"
continues Cannot unring bell, does not overwrite file
on return As with cin to read, will not reread after
function
PARAMETER MUST BE PASS-BY-REFERENCE
![Page 49: CSC 107 – Programming For Science. Today’s Goal Get familiar with opening & closing files Declaring variables for use with files Using variables](https://reader033.vdocuments.net/reader033/viewer/2022052706/5a4d1b1f7f8b9ab059994c4f/html5/thumbnails/49.jpg)
Your Turn
Get into your groups and try this assignment
![Page 50: CSC 107 – Programming For Science. Today’s Goal Get familiar with opening & closing files Declaring variables for use with files Using variables](https://reader033.vdocuments.net/reader033/viewer/2022052706/5a4d1b1f7f8b9ab059994c4f/html5/thumbnails/50.jpg)
For Next Lecture
Another use of data files in Section 17.1-17.3 Must we use text or are there faster
approaches? What was the point of learning binary
numbers? Could we predict size of file we read/write in
program?
Weekly Assignment uses Xcode this week Submit via e-mail; CloudCoder cannot use
files
Programming Assignment #3 available Friday