6
Streams and File I/O in C++ Last Lecture Overview of the four kinds of namespaces in C++ Standard namespace, user-defined namespaces, unnamed namespace, global namespace Reviewed using g++ to compile C++ programs in Linux © J.S. Bradbury CSCI 1060U Lecture 5 Slide 1

Streams and File I/O in C++ - Ontario Tech U · Streams and File I/O in C++ Summary ! Discussed file input and output in C++ using streams ! Writing to files, reading from files,

  • Upload
    others

  • View
    12

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Streams and File I/O in C++ - Ontario Tech U · Streams and File I/O in C++ Summary ! Discussed file input and output in C++ using streams ! Writing to files, reading from files,

Streams and File I/O in C++

Last Lecture §  Overview of the four kinds of namespaces in C++

§  Standard namespace, user-defined namespaces, unnamed namespace, global namespace

§  Reviewed using g++ to compile C++ programs in Linux

© J.S. Bradbury CSCI 1060U Lecture 5 Slide 1

Page 2: Streams and File I/O in C++ - Ontario Tech U · Streams and File I/O in C++ Summary ! Discussed file input and output in C++ using streams ! Writing to files, reading from files,

Streams and File I/O in C++

Overview

§  File input and output in C++ using streams §  Writing to files §  Reading from files §  Appending to files

§  Overloading the << and >> operators for streams

© J.S. Bradbury CSCI 1060U Lecture 5 Slide 2

Page 3: Streams and File I/O in C++ - Ontario Tech U · Streams and File I/O in C++ Summary ! Discussed file input and output in C++ using streams ! Writing to files, reading from files,

File I/O: Writing and Reading

CSCI 1060U Lecture 5 Slide 3 © J.S. Bradbury

See fileio_ex1.cpp for example of how to open a file, write to a file, read from a file and close a file.

See fileio_ex2.cpp for handling file i/o errors gracefully. For example, errors with files not existing or file permission problems.

Page 4: Streams and File I/O in C++ - Ontario Tech U · Streams and File I/O in C++ Summary ! Discussed file input and output in C++ using streams ! Writing to files, reading from files,

File I/O: Appending

CSCI 1060U Lecture 5 Slide 4 © J.S. Bradbury

See fileio_ex3.cpp for reading from a file (line-by-line) and appending to a file.

Page 5: Streams and File I/O in C++ - Ontario Tech U · Streams and File I/O in C++ Summary ! Discussed file input and output in C++ using streams ! Writing to files, reading from files,

Streams and File I/O in C++

Summary §  Discussed file input and output in C++ using

streams §  Writing to files, reading from files, appending to files

§  Learned how to overload the << and >> operators for streams

Next Time §  More file input/output using streams and then

exception handling.

© J.S. Bradbury CSCI 1060U Lecture 5 Slide 5

Page 6: Streams and File I/O in C++ - Ontario Tech U · Streams and File I/O in C++ Summary ! Discussed file input and output in C++ using streams ! Writing to files, reading from files,

Streams and File I/O in C++

Readings §  …

References §  …

© J.S. Bradbury CSCI 1060U Lecture 5 Slide 6