Interactive I/O Input from keyboard Must prompt user User friendly

Slides:



Advertisements
Similar presentations
Getting Data into Your Program
Advertisements

© 2000 Scott S Albert Structured Programming 256 Chapter 7 Streams and File I/O.
1 Text File I/O Chapter 6 Pages File I/O in an Object-Oriented Language Compare to File I/O in C. Instantiate an ofstream object. Like opening.
Files. Advantages of files Can edit data, prepare ahead of time Can rerun file without reentering data Can examine output at leisure Can display output.
CS-1030 Dr. Mark L. Hornick 1 IOStreams revisited Streams, strings, and files.
CPS120: Introduction to Computer Science Lecture 15 B Data Files.
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 Lecture 19 Chapter 4 Program Input and the Software Design Process Dale/Weems/Headington.
Chapter 5: Loops and Files.
1 10/29/07CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
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.
Chapter 15.
Programming with Data Files Chapter 4. Standard Input Output C++ Program Keyboard input cin Output Screen cout.
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.
1 10/25/06CS150 Introduction to Computer Science 1 Reading from and Writing to Files.
Program Input and the Software Design Process ROBERT REAVES.
計算機程式語言 Lecture 8-1 國立臺灣大學生物機電系 8 8 I/O File Streams and Data Files.
Chapter 3 Interactivity and Expressions CSC 125 Introduction to C++ programming.
Chapter 8 Data File Basics.
C++ Basics Structure of a Program. C++ Source Code Plain text file Typical file extension .CPP Must compile the C++ source code without errors before.
Files. Advantages of files Can edit data, prepare ahead of time Can rerun file without reentering data Can examine output at leisure Can display output.
C++ for Engineers and Scientists Second Edition Chapter 8 I/O File Streams and Data Files.
Chapter 9 I/O Streams and Data Files
1 CS161 Introduction to Computer Science Topic #13.
Topics 1.File Basics 2.Output Formatting 3.Passing File Stream Objects to Functions 4.More Detailed Error Testing 5.Member Functions for Reading and 6.Writing.
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.
An Introduction to Programming with C++ Sixth Edition Chapter 14 Sequential Access Files.
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.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 13 File Input and.
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 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.
Loops and Files. 5.1 The Increment and Decrement Operators.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 9. Streams & Files.
1 Chapter 4 Program Input and the Software Design Process.
Declaring fstream Objects An istream object named cin connects program and keyboard An ostream object named cout connects the program and the screen These.
CS162 External Data Files 1 Today in CS162 External Files What is an external file? How do we save data in a file?
Std Library of C++ Part 2. vector vector is a collection of objects of a single type vector is a collection of objects of a single type Each object in.
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.
Files A collection of related data treated as a unit. Two types Text
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.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students File Input and Output Checking input for errors.
1 Stream Input and Output Read Text, page Keyboard and Screen I/O #include cin (of type istream) cout (of type ostream) KeyboardScreen executing.
Computer Programming II Lecture 9. Files Processing - We have been using the iostream standard library, which provides cin and cout methods for reading.
Programming II I/O Streams and Data Files 1(c) Asma AlOsaimi.
Chapter 14: Sequential Access Files
CS-103 COMPUTER PROGRAMMING
Copyright © 2003 Pearson Education, Inc.
File Input/Output.
Parallel Arrays Parallel array =>Two or more arrays with the same number of elements used for storing related information about a collection of data. Example:
Chapter 5: Looping Starting Out with C++ Early Objects Eighth Edition
File I/O.
Lecture 5A File processing Richard Gesick.
Basic File I/O and Stream Objects
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.
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.
CS150 Introduction to Computer Science 1
File I/O in C++ I.
Data Groupings: File File: a group of related records
CS150 Introduction to Computer Science 1
CPS120: Introduction to Computer Science
CHAPTER 4 File Processing.
Reading from and Writing to Files
Reading Data From and Writing Data To Text Files
C++ Programming Lecture 8 File Processing
Dale/Weems/Headington Program Input and the Software Design Process
File I/O in C++ I.
Reading from and Writing to Files
Presentation transcript:

Interactive I/O Input from keyboard Must prompt user User friendly echo checking cosc175/files

Noninteractive processing Batch processing Store values in a file File input and Output C++ can write and read from files just as to keyboard and screen standard input file - keyboard standard output file - screen or printer cosc175/files

Advantages to noninteractive can edit data, prepare ahead of time Can rerun file without reentering data Can examine output at leisure Can display output to screen or print Can use out as input into another program means of communicating between programs cosc175/files

files file - named secondary memory area that holds a collection of information usually stored on an auxiliary storage device examples external file - a file that is used to communicate with people or programs. It is stored externally to the program. internal file - a file that is created but not saved (scratch file) cosc175/files

text files composed of characters only Differs from .doc(x) file Size of file Can be created in notepad, Visual C cosc175/files

File organization naming rules(dos) - 1 to 8 character name, optionally followed by a period and up to a 3 character extension (recommended for programmers!) extension tells the type of file  .cpp – c++ program file .doc – Word file .exe – executable file .obj - Compiled programs - object files .txt - Text files(ASCII form, printable) .dat - data files file is composed of tracks, sectors FAT table cosc175/files

files program files - contains program instructions data files - contains data, collection of records record - one entity i.e. customer file - each record holds data for each customer  fields - data element  customer file has name field, account field,... record key or key field cosc175/files

Path Filename includes path No path – default ddirectory c:\temp\junk.txt Temp directory is usually accessible to students \ is a special character inside strings c:\\temp\\junk.txt cosc175/files

Buffer File i/o is slow Buffer is temporary storage area data for input or output gets written to buffer i/o happens when buffer is full Less reading and writing transaction Happens behind the scenes cosc175/files

Writing Data to a File 1. Open the file as an output file. 2. Write the elements. 3. Close the file (happens automatically when you exit the program) cosc175/files

Example in c++ #include <fstream> //includes ifstream and ofstream //declare a handle ofstream outFile; //output only (writing) //open the file outFile.open("c:\\temp\\stuff.txt"); //outfile is associated with stuff // write to file outFile << "Hello" << endl; cosc175/files

Opening for output causes: ofstream outFile;//any variable can be used here outFile.open(filename); //include path The directory is searched for "filename". If it doesn't exist, it is created. If it does exist, it will be written over The file is set to write mode. A buffer is created. A file pointer is set to the beginning of file. This is updated with each write. cosc175/files

Reading the Data: Open the file for input. Read the data Close the file.  cosc175/files

Opening for input causes: The directory is searched for "filename". The file is set to read mode. A buffer is created in memory. A file pointer is set to the beginning of file. cosc175/files

Example in c++ #include <fstream> //declare a handle ifstream inFile; //input only (reading) //open the file inFile.open("c:\\temp\\stuff.txt"); //DON"T FORGET TO CHECK IF IT WORKED!!! inFile >> name >> age; cosc175/files

Using Files in c++: 1. #include <fstream> 2.declare file streams ifstream inFile; //input only (reading) ofstream outFile; //output only (writing) 3. open each file for reading or writing filevar.open(filename) 4.specify names of file streams in input and output statement cosc175/files

Open Files inMPG.open("inmpg.dat"); outMPG.open("outmpg.dat"); 1. associates a stream variable with file 2. output file – check if it exists, overwrite, if not create sets marker to beginning of file good idea to open all files at the beginning cosc175/files

input and output- use var name in input and output statements in place of cin and cout inFile >> startMiles >> endMiles; outFile << "the mileage per gallon is " << mpg << endl; cosc175/files

Input Failure invalid data -> fail state after fail state, any further i/o operations have no effect opening file that doesn't exist -> fail state Important to test for this!!!!!! inMPG.open("inmpg.dat"); if(!inMPG) { cout << “Can’t open inmpg.dat”; return -1; } cosc175/files