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.

Slides:



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

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.
CPS120: Introduction to Computer Science Lecture 15 B Data Files.
C++ Plus Data Structures
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
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 Programming Style Files Lecture 1 Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus Modified by Reneta Barneva, SUNY-Fredonia.
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++ Vs. Java Who will win?. Look Ma, no classes! C++ was conceived as an object-oriented extension to C C was (is) purely procedural language –Functions.
Streams, Files, and Formatting Chapter Standard Input/Output Streams t Stream is a sequence of characters t Working with cin and cout t Streams.
1 Chapter 4 Program Input and the Software Design Process.
Chapter 8 Data File Basics.
1 Chapter 4 Program Input and the Software Design Process.
1 Chapter 4 Program Input and the Software Design Process Dale/Weems/Headington.
1 Chapter 4 Program Input and the Software Design Process Dale/Weems/Headington.
1 Chapter 4 Program Input and the Software Design Process CS185/09 - Introduction to Programming Caldwell College.
Chapter 1 Software Engineering Principles. Lecture 2.
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.
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.
File I/O 1 ifstreams and ofstreams Sections 11.1 & 11.2.
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.
1 Chapter 4 Program Input and the Software Design Process Dale/Weems/Headington.
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 Chapter 4 Program Input and the Software Design Process Dale/Weems/Headington.
Introducing C++ Programming Lecture 3 Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
1 Cannon_Chapter9 Strings and the string Class. 2 Overview  Standards for Strings  String Declarations and Assignment  I/O with string Variables 
Chapter 5 Conditions, Logical Expressions, and Selection Control Structures Dale/Weems.
1 Chapter 4 Program Input and the Software Design Process Dale/Weems/Headington.
1 Program Input and the Software Design Process. 2 Chapter 4 Topics  Input Statements to Read Values for a Program using >>, and functions get, ignore,
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!"
Loops and Files. 5.1 The Increment and Decrement Operators.
Operating System Using setw and setprecision functions Using setiosflags function Using cin function Programming 1 DCT
Chapter 4 Program Input and the Software Design Process.
Chapter 9: Completing the Basics. In this chapter, you will learn about: – Exception handling – Exceptions and file checking – The string class – Character.
1 What is a Named Constant? A named constant is a location in memory that we can refer to by an identifier, and in which a data value that cannot be changed.
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.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 9. Streams & Files.
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.
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.
Writing to Files and Reading From Files. Writing Data to a File Creating a new file, a.dat or.txt file #include #include // for file writing #include.
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.
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
Copyright © 2003 Pearson Education, Inc.
Chapter 1 Software Engineering Principles
Quiz Next Monday.
File I/O.
Lecture 5A File processing Richard Gesick.
Interactive I/O Input from keyboard Must prompt user User friendly
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
CS150 Introduction to Computer Science 1
File I/O.
Dale/Weems/Headington Program Input and the Software Design Process
Presentation transcript:

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 dataoutput data #include 1

Disk I/O To use disk I/O n Access #include n Choose valid identifiers for your file streams and declare them n Open the files and associate them with disk names 2

Disk I/O, cont... n Use your file stream identifiers in your I/O statements(using >> and <<, manipulators) n Close the files 3

Disk I/O Statements #include ifstream myInfile; // Declarations ofstream myOutfile; myInfile.open(“myIn.dat”); // Open files myOutfile.open(“myOut.dat”); myInfile.close(); // Close files myOutfile.close(); 4

Opening a File Opening a file n Associates the C++ identifier for your file with the physical(disk) name for the file –If the input file does not exist on disk, open is not successful –If the output file does not exist on disk, a new file with that name is created –If the output file already exists, it is erased 5

Opening a File Opening a file n Places a file reading marker at the very beginning of the file, pointing to the first character in the file 6

Stream Fail State l When a stream enters the fail state, n Further I/O operations using that stream have no effect at all n The computer does not automatically halt the program or give any error message 7

Stream Fail State l Possible reasons for entering fail state include: n Invalid input data (often the wrong type) n Opening an input file that doesn’t exist n Opening an output file on a disk that is already full or is write-protected 8

Run Time File Name Entry # include // Contains conversion function c_str ifstream inFile; string fileName; cout << “Enter input file name: “ << endl; // Prompt cin >> fileName; // Convert string fileName to a C string type inFile.open(fileName.c_str()); 9

10 Names in Multiple Formats Problem You are beginning to work on a problem that needs to output names in several formats along with the corresponding social security number. As a start, you decide to write a short C++ program that inputs a social security number and a single name and displays it in the different formats, so you can be certain that all of your string expressions are correct.

11 Algorithm Main ModuleLevel 0 Open files Get social security number Get name Write data in proper formats Close files Open Files Level 1 inData.open("name.dat") outData.open("name.out")

12 Get Name Get first name Get middle name or initial Get last name

13 Write Data in Proper Formats Write first name, blank, middle name, blank, last name, blank, social security number Write last name, comma, first name, blank, middle name, blank, social security number Write last name, comma, blank, first name, blank, middle initial, period, blank, social security number Write first name, blank, middle initial, period, blank, last name

14 Middle initialLevel 2 Set initial to middleName.substr(0, 1) + period Close files inData.close() outData.close()

15 C++ Program //************************************************************* // Format Names program // This program reads in a social security number, a first name // a middle name or initial, and a last name from file inData. // The name is written to file outData in three formats: // 1. First name, middle name, last name, and social security // number. // 2. last name, first name, middle name, and social // security number // 3. last name, first name, middle initial, and social // security number // 4. First name, middle initial, last name //*************************************************************

16 #include // Access ofstream #include // Access string using namespace std; int main() { // Declare and open files ifstream inData; ofstream outData; inData.open("name.dat"); outData.open("name.out");

17 // Declare variables string socialNum;// Social security number string firstName;// First name string lastName;// Last name string middleName;// Middle name string initial;// Middle initial

18 // Read in data from file inData inData >> socialNum >> firstName >> middleName >> lastName; // Access middle initial and append a period initial = middleName.substr(0, 1) + '.';

19 // Output information in required formats outData << firstName << ' ' << middleName << ' ' << lastName << ' ' << socialNum << endl; outData << lastName << ", " << firstName << ' ' << middleName << ' ' << socialNum << endl; outData << lastName << ", " << firstName << ' ' << initial << ' ' << socialNum << endl; outData << firstName << ' ' << initial << ' ' << lastName;

20 // Close files inData.close(); outData.close(); return 0; }