Program Input and the Software Design Process ROBERT REAVES.

Slides:



Advertisements
Similar presentations
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.
Advertisements

CPS120: Introduction to Computer Science Lecture 15 B Data Files.
Chapter 10.
1 Lecture 19 Chapter 4 Program Input and the Software Design Process Dale/Weems/Headington.
Chapter 5: Loops and Files.
Lecture 18:Topic: I/O Formatting and Files Dale/Weems/Headington
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
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.
1 Lecture 5: Input/Output (I) Introduction to Computer Science Spring 2006.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 3: Input/Output.
Chapter 8: I/O Streams and Data Files. In this chapter, you will learn about: – I/O file stream objects and functions – Reading and writing character-based.
How to Program in C++ CHAPTER 3: INPUT & OUTPUT INSTRUCTOR: MOHAMMAD MOJADDAM.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 3: Input/Output.
1 10/25/06CS150 Introduction to Computer Science 1 Reading from and Writing to Files.
Chapter 3: Input/Output
1 Chapter 4 Program Input and the Software Design Process.
STREAMS AND FILES OVERVIEW.  Many programs are "data processing" applications  Read the input data  Perform sequence of operations on this data  Write.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 3: Input/Output.
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
1 CS161 Introduction to Computer Science Topic #13.
File Input and Output in C++. Keyboard and Screen I/O #include cin (of type istream) cout (of type ostream) Keyboard Screen executing program input data.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 3: Input/Output.
File I/O 1 ifstreams and ofstreams Sections 11.1 & 11.2.
An Introduction to Programming with C++ Sixth Edition Chapter 14 Sequential Access Files.
TEXT FILES. CIN / COUT REVIEW  We are able to read data from the same line or multiple lines during successive calls.  Remember that the extraction.
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.
Disk Files for I/O your variable (of type ifstream) your variable (of type ofstream) disk file “myInfile.dat” disk file “myOut.dat” executing program input.
1 Chapter 4 Program Input and the Software Design Process Dale/Weems.
No I/O is built into C++ l instead, a library provides input stream and output stream KeyboardScreen executing program istreamostream 1.
1 Program Input Software Design Chapter 4. 2 You Will Want to Know... Prompting for and reading values into a program. Accessing data from a file. What.
Chapter 3: Input/Output
Loops and Files. 5.1 The Increment and Decrement Operators.
CHAPTER 2 PART #3 C++ INPUT / OUTPUT 1 st Semester King Saud University College of Applied studies and Community Service CSC1101 By: Fatimah.
Chapter 3: Input/Output. Objectives In this chapter, you will: – Learn what a stream is and examine input and output streams – Explore how to read data.
Program Input and the Software Design Process ROBERT REAVES.
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.
1 Chapter 4 Program Input and the Software Design Process.
CS162 External Data Files 1 Today in CS162 External Files What is an external file? How do we save data in a file?
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.
File I/O. Files allow permanent storage of programs and data. ifstream and ofstream objects –For file I/O the inclusion of is required. –Objects for file.
1 Stream Input and Output Read Text, page Keyboard and Screen I/O #include cin (of type istream) cout (of type ostream) KeyboardScreen executing.
Programming II I/O Streams and Data Files 1(c) Asma AlOsaimi.
Chapter 3: Input/Output. Objectives In this chapter, you will: – Learn what a stream is and examine input and output streams – Explore how to read data.
Chapter 4 Program Input and the Software Design Process
ifstreams and ofstreams
Lab 7: File Input/Output Graham Northup
Copyright © 2003 Pearson Education, Inc.
Input and Output Chapter 3.
File I/O.
Standard Input/Output Streams
Lecture 5A File processing Richard Gesick.
Interactive I/O Input from keyboard Must prompt user User friendly
Today’s Lecture I/O Streams Tools for File I/O
Chapter 3: Input/Output
Standard Input/Output Stream
No I/O is built into C++ instead, a library provides input stream and output stream Keyboard Screen executing program istream ostream 1.
Chapter 3 Input output.
CSC 143 Stream I/O Classes and Files [A11-A15, A38-A50]
Chapter 4 Program Input and the Software Design Process
File I/O in C++ I.
CS150 Introduction to Computer Science 1
Reading Data From and Writing Data To Text Files
ifstreams and ofstreams
Dale/Weems/Headington Program Input and the Software Design Process
Chapter 1 c++ structure C++ Input / Output
File I/O in C++ I.
Presentation transcript:

Program Input and the Software Design Process ROBERT REAVES

File Input and Output  File is a named area in a secondary storage that holds a collection of information.  Why would we want to read from a file instead of a keyboard?  Little mistakes.  Easier to enter in large amounts of data multiple times.  Why would we want to write to a file?  Allows us to look at the output over and over without a rerun.  Output of one program can be used as input to another.

Using Files  If we want file I/O in program we have to do:  Request the preprocessor to include the header file fstream.  #include  Use declaration statements to declare the file streams we will use.  Prepare each file for reaching and writing by using a function named open.  Specify the name of the file stream in each input or output statement.

Using Files  Through the header file fstream, the C++ standard library defines two data types, ifstream and ostream.  ifstream represents a stream of characters coming from an input file.  ostream represents a stream of characters going to an output file.  ALL the operators we have learned about cout and cin are valid with these data types.  ifstream uses (>>) operator, get function, ignore function.  ostream uses (<<) operator, endl, setw, setprecision, etc..

Declaring File Streams  Just like we declared int, char, float, etc.. We declare file streams:  int x;  float y;  ifstream myIn;  ostream myOut;  NOTE: Cannot read and write from the same file using the ifstream and the ostream.

Opening Files  Opening a file causes the computer’s operating system to perform certain actions that allow us to proceed with file I/O.  Example of opening a file for reading and writing.  myIn.open(“input.txt”); //Name of the file is irrelevant.  myOut.open(“output.txt”); //Name of the file is irrelevant.  Each of these are a function call to the open function. Both are different open functions, one is associated with the ifstream while the other the ostream.

Open function  Associates a stream variable used in your program with a physical file on disk.  Open, with an input file, sets the file’s reading marker to the first piece of data in the file.  Open, with an output file, checks whether the file already exists.  If it does:  Erases all the old contents of the file. Sets the writing marker to the beginning.  If it doesn’t:  Creates a new, empty file for you. Sets the writing marker to the beginning.  NOTE: Want to open files before any kind of I/O attempts are made.

Close Function  Closing a file causes the operating system to perform certain wrap- up activities on it and to break the connection between the stream variable and the file.  Close function associated with both the ifstream and ostream.  Do we always been to close files?

Run-Time Input of File Names  Open function associated with the ifstream data type requires an argument that specifies the name of the actual data file.  Program will only work with this particular file, we don’t we want! We can to be able to run with any file. =)  Command technique is to prompt the user for the name of the file to read.  ifstream myIn;  string fileName;  cout << “Enter the input file name: “;  cin >> fileName;  myIn.open(fileName); // Compile-time error

Run-Time of File Names  Why do we get a compile-time error?  Open function does not expect an argument of type string, but instead it expects a C string.  We will talk more about C strings later, just KNOW that open doesn’t take string types. =)  Lucky for us our string type does provide a function for us!  c_str, value returning function.  c_str function returns a C string that is equivalent to the one contained in the variable used to call it.  fileName.c_str();  This function is to allow programmers to call library functions that expect C strings and not just string strings. =)

Input Failure  Things can go wrong whenever you input something to a program.\  Prompt user for int value, but we give it some char.  cin will enter a fail state.  Any further I/O operations using that stream are considered null operations.  Computer doesn’t halt program execution or display an error.  Invalid data is the most common reason for input failure.