Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC 107 – Programming For Science. Today’s Goal ALL  Understand why ALL I/O is file I/O  Common bugs to avoid when coding with files in C++  Get a.

Similar presentations


Presentation on theme: "CSC 107 – Programming For Science. Today’s Goal ALL  Understand why ALL I/O is file I/O  Common bugs to avoid when coding with files in C++  Get a."— Presentation transcript:

1 CSC 107 – Programming For Science

2 Today’s Goal ALL  Understand why 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

3 Grandfather of C++  C++ grew out of C programming language  C written 1969 – 1973 to allow creation of Unix OS  Unix OS basis of almost all modern operating systems  Vital status arose from several key features of Unix

4 Important Unix Features  Selected as basis for Internet  Limited hardware requirements  Unable to impregnate the Queen  Easy to port to new computer  Code made freely available  Direct interaction with users  Everything viewed as a file by OS

5 Today's Key Point  Being Unix based, no non-file based I/O in C++  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

6 Today's Key Point

7 What Are cin & cout ?  Statement needed for most file I/O #include  To use cin & cout we must have statement: #include  There is a method: similarity not an accident  cin & cout are file variables defined by system

8 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, ofstream s writes text to a file

9 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 reading at first non-usable value in input  If input is not usable, will set variable equal to 0

10 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

11 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 fancier

12 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 fancier

13 See How Easy It Is? #include #include using namespace std; int main(void) { int sum = 0; int val; cout > val; while (val != -1) { sum += val; cout > val; } return 0; }

14 See How Easy It Is? #include #include using namespace std; int main(void) { ifstream fin; ofstream fout; int sum = 0; int val; fout > val; while (val != -1) { sum += val; fout > val; } return 0; }

15 And When We Run It?

16

17 Must Open File Before Using  Crashes using variable unless refers to open file  When variable declared, open filed named in cString ifstream bobIn("file.txt"); ofstream whyNot(stringFromUser);  Open file later in program using open() routine bobIn.open(nameOfDataFile); whyNot.open("averagesWeCompute");

18 See How Easy It Is? #include #include using namespace std; int main(void) { int sum = 0; int val; cout > val; while (val != -1) { sum += val; cout > val; } return 0; }

19 See How Easy It Is? #include #include using namespace std; int main(void) { ifstream myFile("numbers.txt"); ofstream yNot("sums.out"); int sum = 0; int val; yNot > val; while (val != -1) { sum += val; yNot > val; } return 0; }

20 See How Easy It Is? #include #include using namespace std; int main(void) { ifstream myFile("numbers.txt"); ofstream yNot("sums.out"); int sum = 0; int val; cout > val; while (val != -1) { sum += val; yNot > val; } return 0; }

21 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

22 But…

23

24 Butt…

25 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

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

27 For Next Lecture  Different types of files in Section 17.1 – 17.3  Can we work with files in something other than ASCII?  When to use each of two different ways of using files?  How do we use files in real world program?  Angel also has Weekly Assignment #9 due Tues.  Midterm #2  Midterm #2 in class Friday


Download ppt "CSC 107 – Programming For Science. Today’s Goal ALL  Understand why ALL I/O is file I/O  Common bugs to avoid when coding with files in C++  Get a."

Similar presentations


Ads by Google