File I/O.

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

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 Lecture 19 Chapter 4 Program Input and the Software Design Process Dale/Weems/Headington.
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.
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 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.
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.
1 Chapter 4 Program Input and the Software Design Process Dale/Weems/Headington.
File I/O ifstreams and ofstreams Sections 11.1 &
Chapter 9 I/O Streams and Data Files
1 Streams In C++, I/O occurs in streams. A stream is a sequence of bytes Each I/O device (e.g. keyboard, mouse, monitor, hard disk, printer, etc.) receives.
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.
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.
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 Chapter 4 Program Input and the Software Design Process Dale/Weems/Headington.
Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 4 Working with Data Files.
Streams One of the themes of this course is that everything can be reduced to simple (and similiar) concepts. Streams are one example. Keyboard and Screen.
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.
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?
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.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students File Input and Output Checking input for errors.
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?
1 Stream Input and Output Read Text, page Keyboard and Screen I/O #include cin (of type istream) cout (of type ostream) KeyboardScreen executing.
C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved Chapter Eight: Streams Slides by Evan Gallagher.
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
Chapter 14: Sequential Access Files
ifstreams and ofstreams
I/O Streams File I/O 2-D array review
Functions Manipulating Files
What is a File? A file is a collection on information, usually stored on a computer’s disk. Information can be saved to files and then later reused.
Copyright © 2003 Pearson Education, Inc.
Chapter 1 Software Engineering Principles
Standard Input/Output Streams
Standard Input/Output Streams
Lecture 5A File processing Richard Gesick.
Working with Data Files
Interactive I/O Input from keyboard Must prompt user User friendly
Basic File I/O and Stream Objects
Programming with Data Files
Today’s Lecture I/O Streams Tools for File I/O
when need to keep permanently
Topics Input and Output Streams More Detailed Error Testing
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.
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 from and Writing to Files
ifstreams and ofstreams
Dale/Weems/Headington Program Input and the Software Design Process
File I/O in C++ I.
Reading from and Writing to Files
Presentation transcript:

File I/O

Streams for Input and Output output data input data executing program keyboard disk file screen input stream output stream

Reading Input From Files & Other Input Options Two main types of files: text and binary Text file: comprised of ASCII characters examples ? files you type with Eclipse; files you type with pico, simpletext, notepad* Binary file: structure/contents determined by program which wrote it examples ? MSWord files, jpg, mp3

Example Text File end of line marker int main ( ) { char ch; ch = 'X’; cout << ch; return (0); } <eoln> <eoln> <tab> <eoln> <tab> <eoln> <tab> <eoln> <tab> <eoln> <eoln> end of file marker <eof>

Performing Basic Text file I/O #include <fstream> contains data types ifstream, ofstream, and functions open and close Declare a file stream variable to represent the file you intend to use Open the file you intend to use, linking it to the file stream variable Use new input file stream to read, and/or use new output file stream to write Close the file when done with it ---

What does opening a file do? associates the C++ identifier for your file with the physical (disk) name for the file Forms a link between the two! 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! places a file reading marker at the very beginning of the file, pointing to the first character in it

Doing Input and Output your variable (of type ifstream) #include <fstream> your variable (of type ifstream) (of type ofstream) disk file “C:\myInfile.txt” “C:\myOut.txt” executing program input data output data

Performing Disk Input and Output #include <fstream> contains data types ifstream, ofstream, and functions open and close Declare a file stream variable to represent the file you intend to use Declare one for each direction Open the file you intend to use, linking it to the file stream variable Use new input file stream to read, and/or use new output file stream to write Close the file when done with it ---

The Only Disk I/O Statements You’ll Ever Need #include <fstream> ifstream myInfile; // declarations ofstream myOutfile; myInfile.open(“A:\\myIn.txt”); // open files myOutfile.open(“A:\\myOut.txt”); : myInfile.close( ); // close files myOutfile.close( );

ifstream iostream ofstream Stream input/output ios istream ostream ifstream iostream ofstream fstream

Common Error: read past <eof> marker more reads than data inFile >> i >> j >> ch >> k; effect inFile would enter fail state all subsequent attempts to read from inFile would be skipped i, j, and ch would be as before, k would be garbage ---

Another Common Error: file does not open file name spelled incorrectly inFile.open ( "data.text" ); or, if file is not on the disk effect inFile would enter fail state; all subsequent attempts to read from inFile would be skipped ---

Stream Fail State possible reasons for entering fail state include: invalid input data (often the wrong type) opening an input file that doesn’t exist opening an output file on a diskette that is already full or is write-protected when a stream enters the fail state, further I/O operations using that stream have no effect at all. The computer does not automatically halt the program or give any error message

Example inStream.open(”stuff.dat”); if (inStream.fail()) // or if (!inStream.good()) { //will get here if file does not exist cout << “Input file opening failed.\n”; }

I/O states T F F consists of 3 bits (boolean values) eof fail bad eof() – nonzero value if eofbit is set fail() – nonzero if badbit or failbit is set bad() – nonzero if badbit is set good() – value of 0 (no bits set) T F F

inFile.clear(); //use this to clear all the bad bits! What they mean inFile.eof(): true if eof symbol has been read inFile.fail(): true if operation failed inFile.bad(): true if stream is corrupt inFile.good(): true if no io_flag bits were set, i.e., inFile.good() = !inFile.eof() && !inFile.fail() && !inFile.bad() inFile.clear(); //use this to clear all the bad bits!

When is eof() set? test.dat 3<eof> #include <fstream> int main() { ifstream inData; int val1, val2; inData.open(“test.dat”); inData >> val1; //eof() will be false (0) inData >> val2; //eofbit is set & eof() returns true test.dat 3<eof>

Read a file name and then open we use two types of "strings": C++ standard library and C-style string fileName; cout << "Enter file name -> "; cin >> fileName; inFile.open ( fileName.c_str( ) ); convert to C-style string

Using a loop to check Input cout << “Please enter your input file name:”; cin >> myFileName; inFile.open(myFileName.c_str()); //convert to c_string and opens file while (!inFile.good()) // loop as long as the input isn’t good { inFile.clear(); //puts the file states back to “good” cout << endl << “Invalid file name. Try again:”; }

Useful Experiment Take any of your previous working labs Modify it to take its input from a file Have it check the file stream status for bad input data! Modify it to put its output in another file You can get help from the handouts! (don’t forget to close your files!)