Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 12-1 Today’s Lecture  I/O Streams  Console I/O  File I/O  Tools for File I/O  Sequential.

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

Chapter 12 Streams and File I/O Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Chapter 12 Streams and File I/O. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  I/O Streams  File I/O  Character.
C/C++ Basics (VII) Streams and File I/O Berlin Chen 2003 Textbook: 1. Walter Savitch, “Absolute C++,” Addison Wesley, 2002 開發代理 2. Walter Savitch, “Problem.
Copyright © 2002 Pearson Education, Inc. Slide 1.
File I/O Finished off Colin Cherry standing in for Dr. Lin.
File I/O. COMP104 Lecture 20 / Slide 2 Using Input/Output Files (Review) * A computer file n is stored secondary storage devices (hard drive, CD) n can.
1 11/3/08CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
1 10/29/07CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
1 September 6, 2005CS150 Introduction to Computer Science I What Actions Do We Have Part 1 CS150 Introduction to Computer Science I.
Programming with Data Files Chapter 4. Standard Input Output C++ Program Keyboard input cin Output Screen cout.
 Wednesday, 10/16/02, Slide #1 CS106 Introduction to CS1 Wednesday, 10/16/02  QUESTIONS??  Today:  Return and discuss Test #1  Input from and output.
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.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 6 I/O Streams as an Introduction to Objects and Classes.
String Constructors string str; // creates an empty //string string str(“abc”); // creates a string // from a C-string string str(aString); // creates.
CSIS 123A Lecture 8 Streams & File IO Glenn Stevenson CSIS 113A MSJC.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Chapter 12 Streams and File I/O Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Chapter 12 Streams and File I/O. Learning Objectives I/O Streams – File I/O – Character I/O Tools for Stream I/O – File names as input – Formatting output,
Chapter 8 Data File Basics.
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 &
1 CS161 Introduction to Computer Science Topic #13.
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.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 6 I/O Streams as an Introduction to Objects and Classes.
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.
Input/Output CSci 588: Data Structures, Algorithms and Software Design Fall 2011 All material not from online sources copyright © Travis Desell, 2011
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.
Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Chapter 6 I/O Streams as an Introduction to Objects and Classes.
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?
Lecture 14 Arguments, Classes and Files. Arguments.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Chapter 6 I/O Streams as an Introduction to Objects and Classes.
Copyright © 2002 Pearson Education, Inc. Slide 1.
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.
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?
CSE 232: Moving Data Within a C++ Program Moving Data Within a C++ Program Input –Getting data from the command line (we’ve looked at this) –Getting data.
 Introduction to Computer Science COMP 51 – Fall 2012 – Section 2 File I/O.
Programming II I/O Streams and Data Files 1(c) Asma AlOsaimi.
I/O Streams as an Introduction to Objects and Classes
Chapter 14: Sequential Access Files
What is wrong in the following function definition
ifstreams and ofstreams
What Actions Do We Have Part 1
Auburn University COMP 3000 Object-Oriented Programming for Engineers and Scientists File I/O Dr. Xiao Qin Auburn University.
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.
I/O Streams as an Introduction to Objects and Classes
Basic Input and Output Operations
COMP 2710 Software Construction File I/O
Copyright © 2003 Pearson Education, Inc.
FILE INPUT OUTPUT Skill Area 315 Part B Materials Prepared by
Chapter 6 I/O Streams as an Introduction to Objects and Classes 1.
File I/O.
Basic File I/O and Stream Objects
Programming with Data Files
Today’s Lecture I/O Streams Tools for File I/O
Strings and Streams Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
CSC 143 Stream I/O Classes and Files [A11-A15, A38-A50]
CS150 Introduction to Computer Science 1
File I/O.
CS 144 Advanced C++ Programming February 5 Class Meeting
Reading from and Writing to Files
ifstreams and ofstreams
Reading from and Writing to Files
Presentation transcript:

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 Access & Random Access to Files

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Introduction  Streams  Special objects  Deliver program input and output  In C++  Console I/O is working with streams  File I/O is also working with streams

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Streams  Stream: A flow of data  Input stream  Flow into program  Can come from keyboard  Can come from file  Output stream  Flow out of program  Can go to screen  Can go to file

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Streams Usage  We’ve used streams already  cin  Input stream object connected to keyboard  cout  Output stream object connected to screen

Stream Example int num1, num2, total; cout << "First number: "; cin >> num1; cout << "Second number: "; cin >> num2; total = num1 + num2; cout << “total = " << total << endl; Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 12-5

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Streams Usage  We’ve used streams already  cin  Input stream object connected to keyboard  cout  Output stream object connected to screen  Can define other streams  To or from files  Used similarly as cin, cout

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Streams Usage Examples: cin, cout  Consider:  Using the pre-defined stream cin intNumber; cin >>Number;  Reads value from stream (keyboard), assigned to Number  Using the pre-defined stream cout cout << Number;  Writes value to stream (monitor)

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Streams Usage Like cin, cout  Consider:  Given program defines stream inStream that comes from some file: int Number; inStream >>Number;  Reads value from stream (file), assigned to theNumber  Program defines stream outStream that goes to some file outStream << theNumber;  Writes value to stream, which goes to file

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Files  Reading from file  When program takes input  Writing to file  When program sends output  Start at beginning of file (Sequential access)  Other methods available

Copyright © 2006 Pearson Addison-Wesley. All rights reserved File Connection  Must first create a file stream object  Then connect the file to the file stream object  For input only:  File  ifstream object  For output only:  File  ofstream object  Classes ifstream and ofstream  Defined in library  Named in std namespace

Copyright © 2006 Pearson Addison-Wesley. All rights reserved File I/O Libraries  To allow both file input and output in your program: #include using namespace std;

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Declaring Streams Objects (Variables)  Stream must be declared like any other class variable: ifstream input_file_stream; ofstream out_file_stream;  Must then "connect" to file: input_file_stream.open("infile.txt");  Called "opening the file"  Uses member function open  Can specify complete pathname

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Streams Usage  Once declared and connected, use it like cin and cout! int N; input_file_stream >> N;  Output stream similar: ofstream output_file_stream; output_file_stream.open("outfile.txt"); output_file_stream << "Number = " << N;

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Closing Files  Files should be closed  When program completed getting input or sending output  Disconnects stream from file  In action: input_file_stream.close(); output_file_stream.close();

Copyright © 2006 Pearson Addison-Wesley. All rights reserved File Example: Display 12.1 Simple File Input/Output (1 of 2)

Copyright © 2006 Pearson Addison-Wesley. All rights reserved File Example: Display 12.1 Simple File Input/Output (1 of 2)

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Appending to a File  Standard of stream open operation begins with empty file  Even if file exists  contents lost  Open for append: ofstream outStream; outStream.open("important.txt", ios::app);  If file doesn’t exist  creates it  If file exists  appends to end

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Compare Console I/O and File I/O  All cin functions are same for files!  Read in (send out) integers (int)  Read in (send out) characters (char)  Read in (send out) floating-point s (float)  ….  For String operating:  >> read one word (separated by spaces, new lines)  getline() read one line of words

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Compare Console I/O and File I/O (continued)  For console String (C-string) operations  char S[100]; cin >> S;  read one word  char S[100]; cin.getline(S, 20) ;  read one line of words

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Compare Console I/O and File I/O (continued)  For file String (C-string) operations ifstream ifm; ifm.open("infile.txt");  char S[100]; ifm >> S;  read one word  char S[100]; ifm.getline(S, 20) ;  read one line of words

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Compare Console I/O and File I/O (continued)  cout is same for file operations (say fount) ofstream fout; fout.open(“output.txt", ios::app); cout << “whatever\n” fout << “whatever\n”  Whatever displayed on screen will be same as those saved in file

Copyright © 2006 Pearson Addison-Wesley. All rights reserved More I/O member functions  See Appendix 4 of the textbook Stream_var.open(“File name”); Stream_var.close(); Strean_var.eof(); Stream_var.get(char_var); Stream_var.getline(cstring_var, length);

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Checking End of File (method 1)  Use loop to process file until end  Member function eof() char next; inStream.get(next); while (!inStream.eof()) { cout << next; inStream.get(next); }  Reads each character (including space, new line) until file ends  eof() member function returns bool

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Checking End of File (method 1, continued )  inStream.eof() does not become true until the program attempts to read one character beyond the end of the file, that’s why we need to read a character then check the condition  Consider a file contains AB C inStream.get(next) will be executed 6 times. Why?  Stream_var.getline(cstring_var, length) should also use stream_var.eof() to check end of file

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Checking End of File (method 2)  Use loop to process file until end  An extraction operator actually returns a boolean value inStrem >> next Return true if the read was successful (space and new line will not be read) and returns false when code attempts to read beyond the end of the file Int next, sum =0; while (inStream >> next) { sum = sum + next; } cout << “ the sum is “ << sum << endl;

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Checking End of File (method 2)  Use loop to process file until end inStrem.get(next) Return true if the read was successful and returns false when code attempts to read beyond the end of the file char next, sum =0; while (inStream.get(next)) { sum = sum + 1; } cout << “ the total number of characters is “ << sum << endl;

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Checking End of File (method 2)  Use loop to process file until end inStrem.getline(next, length) Return true if the read was successful and returns false when code attempts to read beyond the end of the file Char[] next, sum =0; while (inStream.get(next, 100)) { sum = sum + 1; } cout << “ the total number of lines is “ << sum << endl;

Which method to use?  I would use method 2! Copyright © 2006 Pearson Addison-Wesley. All rights reserved

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Which Function to Write  The easiest one and works for any text data  <<

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Which Function to Read  Numbers (Integer / floating-point) file: numbers are separated by blank space(s)  >>  Text file: words are separated by blank space(s): each word is going to be processed separately  >>

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Which Function to Read (continued)  Character files: each character (including space) needs to be processed individually  get(char s)  Character files: one line is a record  getline(char s[], int size)

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Tools: File Names as Input  Stream open operation  Argument to open() is string type  Can be literal (used so far) or variable char fileName[16]; ifstream inStream; cout > fileName; inStream.open(fileName);  Provides more flexibility

File Access Applications  Copy files  Copy char by char  Copy line by line  Process files:  Determine how many specific characters  Count words  Count lines  Work on numbers Copyright © 2006 Pearson Addison-Wesley. All rights reserved

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Read and Write can be Performed at the Same Time  Opens same as istream or ostream  Adds second argument  fstream rwStream; rwStream.open("stuff", ios::in | ios:: out);  Opens with read and write capability  It is confusing where to read and where to write: not recommend to use.

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Random Access to Files  Sequential Access  Most commonly used  So far we have talked about  Random Access  Rapid access to records  Access "randomly" to any part of file

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Random Access Tools  Move about in file  rwStream.seekp(1000);  Positions put-pointer at 1000 th byte  rwStream.seekg(1000);  Positions get-pointer at 1000 th byte  Not a requirement for this course

Summary of File Operations  Read from a file  Write to a new or existing file  Delete a file  Copy a file  Move a file  Modify a file Copyright © 2006 Pearson Addison-Wesley. All rights reserved