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.

Slides:



Advertisements
Similar presentations
CSC 107 – Programming For Science. Final Exam Thurs., Dec. 10 th from 8AM – 10AM in OM 221 For exam, plan on using full 2 hours If major problem, come.
Advertisements

CSC 107 – Programming For Science. Final Exam  Fri., Dec. 14 th from 12:30PM – 2:30PM in SH1028  For exam, plan on using full 2 hours  If major problem,
CSC 107 – Programming For Science. Today’s Goal  Get familiar with multi-dimensional arrays  Creating variables for multi-dimensional array  Multi-dimensional.
CS 1620 File I/O. So far this semester all input has been from keyboard all output has been to computer screen these are just two examples of where to.
Chapter 10.
1 11/3/08CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
1 Programming with Data Files Chapter 4 2 Standard Input Output C++ Program Keyboard input cin Output Screen cout.
1 9/1/06CS150 Introduction to Computer Science 1 What Data Do We Have? CS 150 Introduction to Computer Science I.
1 Programming with Data Files Chapter 4. 2 Standard Input Output C++ Program Keyboard input cin Output Screen cout.
Lecture 18:Topic: I/O Formatting and Files Dale/Weems/Headington
1 10/29/07CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
1 September 6, 2005CS150 Introduction to Computer Science I What Actions Do We Have Part 1 CS150 Introduction to Computer Science I.
C++ plus. 2 Goals Some general C++ tips 3 C++ Tips is header file for a library that defines three stream objects Keyboard an istream object named cin.
Programming with Data Files Chapter 4. Standard Input Output C++ Program Keyboard input cin Output Screen cout.
1 10/25/06CS150 Introduction to Computer Science 1 Reading from and Writing to Files.
Programming is instructing a computer to perform a task for you with the help of a programming language.
CSC 107 – Programming For Science. Science Means Solving Problems  Physics – How does an atom work?
CSC 107 – Programming For Science. Today’s Goal  Learn how arrays normally used in real programs  Why a function returning an array causes bugs  How.
STREAMS AND FILES OVERVIEW.  Many programs are "data processing" applications  Read the input data  Perform sequence of operations on this data  Write.
CSC 107 – Programming For Science. Today’s Goal  Discuss writing & using functions  How to declare them, use them, & trace them  Could write programs.
1 CS 101 Lecture 2. 2 Input from the Keyboard Here is a program to accept input from the keyboard and put into into two variables. #include main(){ cout.
CSC 107 – Programming For Science. Today’s Goal Variables  Variable  Variable name location to store data  Only for humans; 0 x 7E8A2410 harder to.
CSC 107 – Programming For Science. Announcements  Memorization is not important, but…  … you will all still be responsible for information  Instead.
C ++ Programming Languages Omid Jafarinezhad Lecturer: Omid Jafarinezhad Fall 2013 Lecture 2 Department of Computer Engineering 1.
CSC 107 – Programming For Science. Today’s Goal  Get familiar with opening & closing files  Declaring variables for use with files  Using variables.
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
ITEC 320 C++ Examples.
CSC 107 – Programming For Science. Today’s Goal  Discuss how to hand data to functions  Review loopholes in variables & scoping rules  Ways to get.
C++ for Engineers and Scientists Second Edition Chapter 8 I/O File Streams and Data Files.
File I/O ifstreams and ofstreams Sections 11.1 &
Chapter 9 I/O Streams and Data Files
CSC 107 – Programming For Science. History of C  Dennis Ritchie developed C from 1969 – 1973  While at Bell Labs, created language to develop Unix 
File I/O 1 ifstreams and ofstreams Sections 11.1 & 11.2.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT11: File I/O CS2311 Computer Programming.
CSC 107 – Programming For Science. Today’s Goal  Discuss writing functions that return values  return statement’s meaning and how it works  When and.
CSC 107 – Programming For Science. Announcements.
CSC 107 – Programming For Science. Today’s Goal  Better understand arrays and how they work  Using array variable & its entries  When calling function,
CS Class 03 Topics  Sequence statements Input Output Assignment  Expressions Read pages Read pages 40 – 49 for next time.
Chapter 11 Standard C++ Strings and File I/O Dept of Computer Engineering Khon Kaen University.
CSC 107 – Programming For Science. Today’s Goal  Get familiar with opening & closing files  Declaring variables for use with files  Using variables.
Input/Output CSci 588: Data Structures, Algorithms and Software Design Fall 2011 All material not from online sources copyright © Travis Desell, 2011
Input/Output. Objectives In this chapter you will: Learn what a stream is and examine input and output streams Explore how to use the input stream functions.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Today’s Lecture  I/O Streams  Console I/O  File I/O  Tools for File I/O  Sequential.
File I/O in C++. Using Input/Output Files A computer file  is stored on a secondary storage device (e.g., disk);  is permanent;  can be used to provide.
File I/O in C++ I. Using Input/Output Files A computer file is stored on a secondary storage device (e.g., disk); is permanent; can be used to provide.
1 Stream Input and Output Read Text, page Keyboard and Screen I/O #include cin (of type istream) cout (of type ostream) KeyboardScreen executing.
CSC 107 – Programming For Science. Today’s Goal  Write functions that take & return values  How parameters declared and how we call functions  What.
PHY 107 – Programming For Science. Today’s Goal  Get familiar with opening & closing files  Declaring variables for use with files  Using variables.
ifstreams and ofstreams
CMPT 201.
What Actions Do We Have Part 1
Computing Fundamentals
Copyright © 2003 Pearson Education, Inc.
Basic File I/O and Stream Objects
Functions A function is a “pre-packaged” block of code written to perform a well-defined task Why? Code sharing and reusability Reduces errors Write and.
Today’s Lecture I/O Streams Tools for File I/O
Text Files All the programs you've seen so far have one thing in common: Any data the program uses or calculates is lost when the program ends. In order.
Chapter 3: Input/Output
File I/O in C++ I.
File I/O.
What Actions Do We Have Part 1
Reading from and Writing to Files
Programming with Data Files
Using string type variables
ifstreams and ofstreams
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Chapter 1 c++ structure C++ Input / Output
File I/O in C++ I.
Reading from and Writing to Files
Presentation transcript:

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 better understanding of what >> & << do  cin, cout are variables: know what their types are

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

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

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

Today's Key Point

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

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

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

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

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

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

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

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

And When We Run It?

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

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

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

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

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

But…

Butt…

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

Your Turn  Get into your groups and try this assignment

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