File I/O.

Slides:



Advertisements
Similar presentations
Chapter 12 Streams and File I/O Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Advertisements

Chapter 12 Streams and File I/O. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  I/O Streams  File I/O  Character.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Programming File I/O. COMP102 Prog. Fundamentals File I/O / Slide 2 Copyright © 2000 by Brooks/Cole Publishing Company A division of International Thomson.
1 Engineering Problem Solving With C++ An Object Based Approach Chapter 4 Programming with Data Files.
File I/O. COMP104 Lecture 20 / Slide 2 Using Input/Output Files * A computer file n is stored on a secondary storage device (e.g., disk) n is permanent.
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.
File I/O. COMP104 File I/O / Slide 2 Using Input/Output Files * A computer file n is stored on a secondary storage device (e.g., disk) n is permanent.
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.
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.
Programming File I/O. COMP102 Prog. Fundamentals File I/O / Slide 2 Copyright © 2000 by Brooks/Cole Publishing Company A division of International Thomson.
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.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 6 I/O Streams as an Introduction to Objects and Classes.
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,
File I/O ifstreams and ofstreams Sections 11.1 &
C++ Introduction : C++ compilation. Visual Studio 2008 : Creating Command-Line Program.
File handling in C++ BCA Sem III K.I.R.A.S. Using Input/Output Files Files in C++ are interpreted as a sequence of bytes stored on some storage media.
File I/O 1 ifstreams and ofstreams Sections 11.1 & 11.2.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT11: File I/O CS2311 Computer Programming.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 6 I/O Streams as an Introduction to Objects and Classes.
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!"
File Handling in C++.
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.
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.
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++. 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.
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.
 Introduction to Computer Science COMP 51 – Fall 2012 – Section 2 File I/O.
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.
I/O Streams as an Introduction to Objects and Classes
File Processing in C++ Data is stored in files so that it may be retrieved for processing when needed.
CS-103 COMPUTER PROGRAMMING
Auburn University COMP 3000 Object-Oriented Programming for Engineers and Scientists File I/O Dr. Xiao Qin Auburn University.
I/O Streams as an Introduction to Objects and Classes
Lab 7: File Input/Output Graham Northup
COMP 2710 Software Construction File I/O
Copyright © 2003 Pearson Education, Inc.
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.
Quiz Next Monday.
Chapter 6 I/O Streams as an Introduction to Objects and Classes 1.
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
Strings and Streams Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
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.
Standard Input/Output Stream
File Processing in C++ Data is stored in files so that it may be retrieved for processing when needed.
File I/O.
File I/O in C++ I.
File I/O.
Programming File I/O.
CS149D Elements of Computer Science
CS 144 Advanced C++ Programming February 5 Class Meeting
CHAPTER 4 File Processing.
Programming with Data Files
File Streams 12/09/13.
File I/O in C++ I.
Presentation transcript:

File I/O

Stream A stream is a flow of data. Input stream: Data flows into the program cin: Input stream connected to the keyboard Output stream: Data flows out of the program cout:Output stream connected to the screen

File I/O Reading from a file Writing to a file Taking input from a file Writing to a file Sending output to a file

Why Use Files? Files allow you to store data permanently! An input file can be used over and over No typing of data again and again for testing Create a data file or read an output file at your convenience Files allow you to deal with larger data sets

Declaring Stream Variables iostream Include it, referencing its objects fstream include and using directives #include <fstream> using namespace std; Declare an input-file stream variable using ifstream in_stream; (Input-file streams are of type ifstream) Declare an output-file stream variable using ofstream out_stream; (Output-file streams are of type ofstream)

Connecting To a File Once a stream variable is declared, connect it to a file Connecting a stream to a file is opening the file Use the open function of the stream object in_stream.open("infile.dat"); Double quotes Period File name on the disk

Using I/O file stream Once connected to a file, the input-stream variable can be used to produce input just as you would use cin with the extraction operator Input Example: int one_number, another_number; in_stream >> one_number >> another_number; Output Example: ofstream out_stream; out_stream.open("outfile.dat"); out_stream << "one number = "<< one_number << "another number = " << another_number;

Closing a File After using a file, it should be closed This disconnects the stream from the file Close files to reduce the chance of a file being corrupted if the program terminates abnormally input_file.close(); It is important to close an output file if your program later needs to read input from the output file The system will automatically close files if you forget as long as your program ends normally

example File_io_example