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.

Slides:



Advertisements
Similar presentations
This Time Whitespace and Input/Output revisited The Programming cycle Boolean Operators The “if” control structure LAB –Write a program that takes an integer.
Advertisements

L which include file needs to be used for string manipulation? what using statement needs to be included fro string manipulation? l how is a string assigned.
CMSC 2021 C++ I/O and Other Topics. CMSC 2022 Using C++ Stream I/O Default input stream is called cin Default output stream is called cout Use the extraction.
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.
© 2000 Scott S Albert Structured Programming 256 Chapter 7 Streams and File I/O.
Chapter 12 Streams and File I/O Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Chapter 5: Loops and Files.
1 Programming with Data Files Chapter 4. 2 Standard Input Output C++ Program Keyboard input cin Output Screen cout.
1 Lecture 20:Arrays and Strings Introduction to Computer Science Spring 2006.
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.
Chapter 8: I/O Streams and Data Files. In this chapter, you will learn about: – I/O file stream objects and functions – Reading and writing character-based.
CS 1 Lesson 5 Loops and Files CS 1 -- John Cole.
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.
Darbas su failais Arnas Terekas IT 1gr. Vilniaus universitetas Matematikos ir informatikos fakultetas.
CSIS 123A Lecture 8 Streams & File IO Glenn Stevenson CSIS 113A MSJC.
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 &
Chapter 9 I/O Streams and Data Files
1 CS161 Introduction to Computer Science Topic #13.
File I/O 1 ifstreams and ofstreams Sections 11.1 & 11.2.
4. Input/Output Intro Programming in C++ Computer Science Dept Va Tech August, 2001 © Barnette ND & McQuain WD 1 C++ Input/Output: Streams The.
Loops and Files. 5.1 The Increment and Decrement Operators.
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
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.
Learners Support Publications Working with Files.
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.
Streams and File Processing in C++ Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
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.
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.
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?
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.
CS212: Object Oriented Analysis and Design
ifstreams and ofstreams
CS-103 COMPUTER PROGRAMMING
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
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.
Data Streams.
File I/O.
Standard Input/Output Streams
Standard Input/Output Streams
Lecture 5A File processing Richard Gesick.
File I/O.
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.
Topics Input and Output Streams More Detailed Error Testing
Standard Input/Output Stream
Chapter 3 Input output.
CSC 143 Stream I/O Classes and Files [A11-A15, A38-A50]
CS150 Introduction to Computer Science 1
File I/O in C++ I.
CS150 Introduction to Computer Science 1
File I/O.
ifstreams and ofstreams
Lecture 3 More on Flow Control, More on Functions,
File I/O in C++ I.
Presentation transcript:

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 I/O must be created: For file output a variable/object of type ofstream will be created and will be used like cout For file input a variable/object of type ifstream will be created and will be used like cin –Any valid variable name may be used for the variables. Two Names Involved in File I/O –There are two names that must be distinguished between: 1.variable name - the name used to access the file object in the program 2.filename - the name the file is known by in the operating system

Opening and Closing Files Opening a file connects the file with specified filename to an input or output stream (variable), if successful –Opening a file causes a request to be made to the operating system to open the file –The operating system is in charge of files and does things like checking access permissions setting up a data structure for the opened file that includes information such as the location for the next read or write Closing the file object terminates access to the file by the program and causes the operating system "disconnect" it from the program.

Opening and Closing Files in a Program When an ifstream or an ofstream object is defined filename may be specified and then the file will be opened (if there is no error) if no filename is specified then file opening must be done as a separate step Opening a file fails if: file does not exist or program does not have permission to access the file It is necessary to check if it the file was open successfully

Defining fstream objects: 2-step example input/output stream needs to be declared: ifstream in_stream; // input stream ofstream out_stream; // output stream opening the file: in_stream.open(”myfile.dat”); // opening file -before C++ program can read from or write to a file stream, the stream needs to be connected to a file Note: string variable can be used for file name ( c_str() function has to be applied on the C++ string) //2-step example string filename=“myfile.dat”; ifstream in_stream; in_stream.open(filename.c_str());

Working with fstream objects after stream variable has been connected to a file, extraction and insertion operators can be used with a stream: in_stream >> var1 >> var2 >> var3; or out_stream << var1 << var2 << var3; after opening the file all the operations on the file is done through associated stream before program finishes every stream needs to closed: in_stream.close(); // closing file

I/O Error Checking Function fail() returns true if previous stream operation failed useful to check if file is opened successfully before performing operations on file Function exit() - exists program immediately To use exit, include exit() accepts one integer as argument: –convention: 0 - normal program termination; nonzero - abnormal Example in_stream.open(“myfile.dat”); // opening file if (in_stream.fail()){ cout << “Cannot open file myfile.dat”; exit(1); }

Defining ifstream object: 1-step example Again, in may be used in the same manner cin is used.

Defining ofstream object – 1-step example Again, out may be used in the same manner cout is used.

Inputting and Outputting Inputting The >> operator reads a whitespace delimited sequence of built-in types and objects (if >> has been defined for the object) Any of the built-in types may be read in using >> –also C-style strings, C++ strings, and complex numbers Two general conditions under which an input stream evaluates to false: –The end-of-file is read. The end-of-file is indicated internally in an operating system dependent manner –An invalid character is encountered. In this case the input stream goes into a state of error and all reading of input ceases Outputting Outputting may be done in the usual manner with the << operator.

Stream Objects as Parameters Streams must be passed to functions by-reference so that a copy is not made If output is done with the reference parameter –the same stream is used in the called function If a copy is made (pass-by-value) –the stream in the calling function does not change when the called function does I/O –called function uses the copy and the original stream (in the calling function) will probably not work correctly afterwards