Standard Input/Output Stream

Slides:



Advertisements
Similar presentations
Getting Data into Your Program
Advertisements

File Input/Output. External Files Batch –Requires use of data files (save to disk) –Batch can be run during off peak use –allows things to be complete.
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.
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.
1 Engineering Problem Solving With C++ An Object Based Approach Chapter 4 Programming with 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 Programming with Data Files Chapter 4. 2 Standard Input Output C++ Program Keyboard input cin Output Screen cout.
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.
Programming with Data Files Chapter 4. Standard Input Output C++ Program Keyboard input cin Output Screen cout.
This set of notes is adapted from that provided by “Computer Science – A Structured Programming Approach Using C++”, B.A. Forouzan & R.F. Gilberg, Thomson.
1 10/25/06CS150 Introduction to Computer Science 1 Reading from and Writing to Files.
Streams, Files. 2 Stream Stream is a sequence of bytes Input stream In input operations, the bytes are transferred from a device to the main memory Output.
Program Input and the Software Design Process ROBERT REAVES.
C ++ Programming Languages Omid Jafarinezhad Lecturer: Omid Jafarinezhad Fall 2013 Lecture 2 Department of Computer Engineering 1.
Chapter 10C++ for Java Programmers1 Chapter 10 Input/Output.
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.
Define our own data types We can define a new data type by defining a new class: class Student {...}; Class is a structured data type. Can we define our.
COP 3530 Data Structures & Algorithms Discussion Session 3.
Quiz // // The function exchanges the two parameters. // Param: ( ) // Param:
Quiz 2 Results. What Is Wrong? #include using namespace std int Main() { // Say Hello 4 times for(i == 0; i < 3; i++) { cout >> "Hello World!"
Input/Output CSci 588: Data Structures, Algorithms and Software Design Fall 2011 All material not from online sources copyright © Travis Desell, 2011
File Handling in C++.
Program Input and the Software Design Process ROBERT REAVES.
Lecture 14 Arguments, Classes and Files. Arguments.
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.
Advanced File Operations Chapter File Operations File: a set of data stored on a computer, often on a disk drive Programs can read from, write to.
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.
CS 1430: Programming in C++ 1. File Input in VS Project Properties Debugging Command Arguments quiz8-1.out We want to know how to do it ourselves, right?
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.
CS212: Object Oriented Analysis and Design
MT262A Review.
File Processing in C++ Data is stored in files so that it may be retrieved for processing when needed.
Auburn University COMP 3000 Object-Oriented Programming for Engineers and Scientists File I/O Dr. Xiao Qin Auburn University.
Lab 7: File Input/Output Graham Northup
Copyright © 2003 Pearson Education, Inc.
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:
Review of Strings which include file needs to be used for string manipulation? what using statement needs to be included fro string manipulation? how is.
FILE INPUT OUTPUT Skill Area 315 Part B Materials Prepared by
Quiz Next Monday.
CS 1430: Programming in C++.
File I/O.
Standard Input/Output Streams
Standard Input/Output Streams
More About File Reading
Programming with Data Files
Today’s Lecture I/O Streams Tools for File I/O
when need to keep permanently
Review of Strings which include file needs to be used for string manipulation? what using statement needs to be included for string manipulation? how is.
File Processing in C++ Data is stored in files so that it may be retrieved for processing when needed.
Chapter 3 Input output.
CS150 Introduction to Computer Science 1
File I/O in C++ I.
CS150 Introduction to Computer Science 1
File I/O.
CPS120: Introduction to Computer Science
CHAPTER 4 File Processing.
Reading from and Writing to Files
Programming with Data Files
File Streams 12/09/13.
COMS 261 Computer Science I
File I/O in C++ II.
File I/O in C++ I.
Reading from and Writing to Files
Presentation transcript:

Standard Input/Output Stream // Header file #include <iostream> int size; float avg; cin >> size; // cin: Standard input stream // Processing cout << "Average is " << avg; // cout: standard output stream -----

Header File for File I/O // File Stream #include <fstream> // Input File Stream struct ifstream // should be class { … } // Output File Stream struct ofstream // should be class -----

File Input/Output #include <fstream> int main() { ifstream MyInput; ofstream yourOutFile; int x; MyInput.open(“P6.IN"); yourOutFile.open(“p6.out”); if (MyInput.fail() || !P6Out.good()) cout << "Error: Cannot open files!"; return 1; } MyInput >> x; while (!MyInput.eof()) x ++; yourOutFile << x; MyInput.close(); yourOutFile.close(); return 0;

File Input/Output // DO_01: Include header file for file I/O int main() { // DO_02: declare variables for file I/O // DO_03: open file // DO_04: Check open operation // DO_05: Read value from the open file // DO_06: Fill condition: read until the end of file while ( ) // DO_07: Output value to a file // Read the next value from the file } // DO_08: Close files -----

Declare and Initialize Stream Variables #include <fstream> int main() { ifstream MyInput(“P6.in"); ofstream P6Out(“P6.out"); if (MyInput.fail() || !P6Out.good()) cout << "Error: Cannot open files!"; return 1; } // Do work MyInput.close(); P6Out.close(); return 0; -----

Input File Name #include <fstream> int main() { char file_name[21]; // Cannot use data type string for file_name ifstream MyInput; cout << “Enter input file name: ”; cin >> file_name; MyInput.open(file_name); // MyInput.open(“P6.IN"); // Check open operation if (!MyInput.good()) cout << "Error: Cannot open input file!"; return 1; } // Do work MyInput.close(); return 0;

Selecting I/O Streams #include <fstream> int main() { ifstream MyInput; ofstream yourOutFile; // Open files for I/O // Check open operations cin = MyInput; cout = yourOutFile; cin >> x; cout << ++x; cout << x++; // Close files return 0; }

File Stream as Function Parameter int main() { ifstream MyInput; if (!OpenFile(MyInput)) return 1; return 0; } bool OpenFile(ifstream& inFile) char file_name[21]; cin >> file_name; inFile.open(file_name); if (!inFile.good()) cout << "Error: Cannot open input file"; return false; else return true; -----

This Week Today Lab10 / Quiz10-1 / Prog6 Thursday Lab11 / Prog6 / Test 3 Friday No Class