23filesAdvIO_3014

23filesAdvIO_3014 - Lecture File 23 COP 3014 January 17,...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
Lecture File 23 COP 3014 January 17, 2008 A. Ford Tyson 1 COP 3014 Lecture File 23 ± More about Files and Advanced IO – IO stream classes and modes 1 – Stream states and bit flags – IO functions for char and string – Binary files – Random access – Opening a file for both input and output Copyright 1997-present, Ann Ford Tyson Stream Concept ± Stream: sequence of characters with functions to take characters out of one end, and put characters into the other end 2 keyboard program file program display program ± Two main types of files: text and binary ± Text file: comprised of ASCII characters examples ? IO and Files 3 ± Binary file: structure/contents determined by program which wrote the file examples ? files you type with pico, simpletext, notepad MSWord files, jpg, mp3
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Lecture File 23 COP 3014 January 17, 2008 A. Ford Tyson 2 Examples: programs which read and write to files ± Word Processors ± Games: read initial state of game, write updated state when quit 4 ± DBMS (database management systems): keep large collections of data, e.g. at a sales web site or hospital ± Compilers: read your .cpp file and write to a .exe file (as well as other files) ± Etc. IO Classes and Inheritance preview ± The IO data structures in C++ are defined as classes ± ios_base is the overall parent class for IO stream classes 5 ± inheritance: a class may be derived from another class, in which case the derived class will inherit some of its features ± we are simplifying some IO concepts and capabilities of C++ in this lecture IO Classes and Inheritance preview ios_base 6 ostream (cout) ofstream istream (cin) ifstream An object of type ifstream is also an object of type istream, etc. iostream fstream
Background image of page 2
Lecture File 23 COP 3014 January 17, 2008 A. Ford Tyson 3 Using Files ± #include <fstream> to obtain access to IO classes needed ± fstream object – can be used for both input and output 7 ± ifstream object – can be used for input – when opened, defaults to input ± ofstream object – can be used for output – when opened, defaults to output fstream ± can specify mode in the open call ± Basic modes are (note :: is the "scope resolution operator") 8 – ios::in – ios::out ± Example – fstream myfile; – myfile.open ("data.txt", ios::in); File Access modes p.1 ± ios::app – Append mode. If the file exists, contents are preserved and data is written to the end of the file. If the file does not exist it is created 9 If the file does not exist, it is created. ± ios::ate – If the file exists, the write pointer goes to the end of it. Data can be written anywhere in the file.
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Lecture File 23 COP 3014 January 17, 2008 A. Ford Tyson 4 File Access flags p.2 ± ios::binary – Data is written to the file in binary format (files default to text format) 10 ± ios::in – Input mode. If the file does not exist, the open operation fails.
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 03/14/2011 for the course COP 3014 taught by Professor Tyson during the Fall '10 term at FSU.

Page1 / 16

23filesAdvIO_3014 - Lecture File 23 COP 3014 January 17,...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online