lec11-io - 1 CSC 1111 Introduction to Computing using C++...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 1 CSC 1111 Introduction to Computing using C++ Stream I/O and File I/O 2 Outlines Basics of Stream I/O File I/O vs. Console I/O Class ifstream and ofstream Input/Output methods in istream/ostream and ifstream/ofstream 3 Introduction What is File I/O? What is Stream I/O? Why do we need File I/O? 4 Keyboard Console cin (An istream object) cout (An ostream object) C++ Program istream and ostream objects act as "middle persons" between C++ programs and the I/O sources (e.g.: Keyboard, console screen) These objects offer methods and overloaded operators for C++ programs to perform both low-level and high-level I/O . Stream of bytes Stream of bytes Bytes or converted high-level data Bytes or High-level data Abstraction of I/O (Console I/O) 5 Low- and High- Level I/O Low-level I/O Unformatted Individual byte unit of interest High speed Suitable for use with binary files e.g., JPEG Image, MPEG Video files, etc. High-level I/O Formatted Bytes grouped into meaningful units: integers, characters, etc. Convenient to use but slower Suitable for plain-text files (files containing only printable ASCII characters) 6 Disk input.txt output.txt ifstream object ofstream object C++ Program To perform file I/O, we use ifstream/ofstream objects. ifstream/ofstream are subclasses of istream/ostream they offer everything istream/ostream offer plus some file-specific methods. Stream of bytes Stream of bytes Bytes or converted high-level data Bytes or High-level data Abstraction of I/O (File I/O) 7 Filenames and Directory Structures Relative filename Relative to the folder where you run the program file1.txt ..\Folder B\file2.txt Absolute filename Full path (up to the root) + filename C:\Folder C\file1.txt C:\Folder B\file2.txt f Local Disk (C:) f Folder_A f Folder_B f file1.txt f file2.txt f Folder_C f file1.txt f program.exe Remember to represent \ as \\ in C++ source program. On Unix and some other OS, paths are separated by ( / ) instead of ( \ ) 8 File I/O Reading data from a file is similar to reading data from a keyboard Characters are read from the "input source" character by character sequentially. Keyboard input: May have to wait for additional input File Input: All data are in the file. Writing data to a file is similar to printing data to the console. Characters are output character by character sequentially. 9 Steps for performing File I/O 1. Prepare a file for reading/writing by creating a file object 2. Reading/writing data from/to file 3. When done with File I/O, close the file Closing a file means releasing all the resources in the memory associated with the file....
View Full Document

Page1 / 40

lec11-io - 1 CSC 1111 Introduction to Computing using C++...

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

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