Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "CSC 107 – Programming For Science. Today’s Goal  Get familiar with opening & closing files  Declaring variables for use with files  Using variables."— Presentation transcript:

1 CSC 107 – Programming For Science

2 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

3 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 result

4 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

5 This Is (Semi-Real) Problem  Large hadron collider about to come on-line  No black hole when smashing particles at high speeds 28.5 GB/min  Creates 28.5 GB/min for nerds seeking truth & beauty

6 This Is (Semi-Real) Problem  Large hadron collider about to come on-line  No black hole when smashing particles at high speeds 28.5 GB/min  Creates 28.5 GB/min for nerds seeking truth & beauty  Hired trained monkeys to type data into programs

7 This Is (Semi-Real) Problem  Large hadron collider about to come on-line  No black hole when smashing particles at high speeds 28.5 GB/min  Creates 28.5 GB/min for nerds seeking truth & beauty  Hired trained monkeys to type data into programs college students

8 This Is (Semi-Real) Problem  Large hadron collider about to come on-line  No black hole when smashing particles at high speeds 28.5 GB/min  Creates 28.5 GB/min for nerds seeking truth & beauty  Hired trained monkeys to type data into programs college students

9 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

10 Program Basics For Files  All built-in file I/O code means adding to header #include #include  Place with other #include s 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

11 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

12 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

13 File Variable Declaration

14

15

16 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

17 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

18 Opening the File  Can open file when variable declared char 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 opened ifstream because; ofstream isaidso; because.open("mightMakes.right"); cin >> sndName; isaidso.open(sndName);

19 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

20 Examples of is_open char sndName[]; ifstream because, foo("foo.txt"); ofstream isaidso, bar("snafu"); cout > sndName; isaidso.open(sndName); cout << because.is_open() << endl; cout << isaidso.is_open() << endl;

21 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?

22 What Happens To Existing File?

23 Oops!

24 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

25 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

26 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 reached ofstream phillies("nlChamp"); ifstream yankees("evilEmpire"); phillies.close(); yankees.close();

27 Your Turn  Get into your groups and try this assignment

28 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  Midterm #2 in class next Friday


Download ppt "CSC 107 – Programming For Science. Today’s Goal  Get familiar with opening & closing files  Declaring variables for use with files  Using variables."

Similar presentations


Ads by Google