Streams - Object input and output in C++

Similar documents
Module 11 The C++ I/O System

basic_fstream<chart, traits> / \ basic_ifstream<chart, traits> basic_ofstream<chart, traits>

Piyush Kumar. input data. both cout and cin are data objects and are defined as classes ( type istream ) class

Chapter 12 - C++ Stream Input/Output

Fig: iostream class hierarchy

Input and Output. Data Processing Course, I. Hrivnacova, IPN Orsay

I/O Streams and Standard I/O Devices (cont d.)

Standard I/O in C and C++

Java IO and C++ Streams

CS2141 Software Development using C/C++ Stream I/O

Chapter 3 - Notes Input/Output

COMP322 - Introduction to C++

Objects and streams and files CS427: Elements of Software Engineering

UNIT V FILE HANDLING

Introduction to C++ (Extensions to C)

Object Oriented Programming Using C++ UNIT-3 I/O Streams

C++ Programming Lecture 10 File Processing

C++ Input/Output: Streams

Physics 6720 I/O Methods October 30, C++ and Unix I/O Streams

C++ does not, as a part of the language, define how data are sent out and read into the program

CS201 Solved MCQs.

Convenient way to deal large quantities of data. Store data permanently (until file is deleted).

Writing a Good Program. 7. Stream I/O

Engineering Problem Solving with C++, Etter/Ingber

by Pearson Education, Inc. All Rights Reserved. 2

Strings and Streams. Professor Hugh C. Lauer CS-2303, System Programming Concepts

Consider the following example where a base class has been derived by other two classes:

Intermediate Programming Methodologies in C++ CIS22B. Joe Bentley

Lecture 5 Files and Streams

The C++ Language. Output. Input and Output. Another type supplied by C++ Very complex, made up of several simple types.

Object Oriented Programming In C++

Jan 27, C++ STL Streams. Daniel Maleike

Streams. Rupesh Nasre.

Input/Output Streams: Customizing

Chapter 12. Streams and File I/O. Copyright 2010 Pearson Addison-Wesley. All rights reserved

Module C++ I/O System Basics

This chapter introduces the notion of namespace. We also describe how to manage input and output with C++ commands via the terminal or files.

Chapter 3: Input/Output

Introduction. Lecture 5 Files and Streams FILE * FILE *

Random File Access. 1. Random File Access

Fundamentals of Programming Session 27

IS 0020 Program Design and Software Tools

BITG 1233: Introduction to C++

File I/O Christian Schumacher, Info1 D-MAVT 2013

What we will learn about this week:

Chapter 21 - C++ Stream Input/Output

Chapter 12. Streams and File I/O. Copyright 2016 Pearson, Inc. All rights reserved.

Unit-V File operations

COMP322 - Introduction to C++

Chapter 21 - C++ Stream Input/Output

C++ Basics. Lecture 2 COP 3014 Spring January 8, 2018

Setting Justification

Lecture 9. Introduction

Week 3: File I/O and Formatting 3.7 Formatting Output

UEE1303(1070) S 12 Object-Oriented Programming in C++

THE INTEGER DATA TYPES. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

C++ Binary File I/O. C++ file input and output are typically achieved by using an object of one of the following classes:

We will exclusively use streams for input and output of data. Intro Programming in C++

Chapter 2. Outline. Simple C++ Programs

CSc Introduc/on to Compu/ng. Lecture 19 Edgardo Molina Fall 2011 City College of New York

Advanced I/O Concepts

The C++ Input/Output Class Hierarchy

Chapter 14 Sequential Access Files

Today in CS162. External Files. What is an external file? How do we save data in a file? CS162 External Data Files 1

Streams in C++ Stream concept. Reference information. Stream type declarations

Stream States. Formatted I/O

After going through this lesson, you would be able to: store data in a file. access data record by record from the file. move pointer within the file

Chapter 8 File Processing

c++ keywords: ( all lowercase ) Note: cin and cout are NOT keywords.

Chapter 12. Streams and File I/O

[CSE10200] Programming Basis ( 프로그래밍기초 ) Chapter 7. Seungkyu Lee. Assistant Professor, Dept. of Computer Engineering Kyung Hee University

Software Design & Programming I

Getting started with C++ (Part 2)

Streams. Parsing Input Data. Associating a File Stream with a File. Conceptual Model of a Stream. Parsing. Parsing

Generate error the C++ way

I/O Streams. Stream Input. I/O Stream Manipulators. State-of-Stream Member Functions. I/O Stream Format Flag Member Functions

File I/O. File Names and Types. I/O Streams. Stream Extraction and Insertion. A file name should reflect its contents

String Variables and Output/Input. Adding Strings and Literals to Your Programming Skills and output/input formatting

More File Operations. Lecture 17 COP 3014 Spring april 18, 2018

Lecture 3 The character, string data Types Files

Definition Matching (10 Points)

CS 151 Review #3. // More than one variable can be defined // in a statement. Multiple variables are // separated by a comma.

Chapter 2: Basic Elements of C++

CS3157: Advanced Programming. Outline

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Objectives. In this chapter, you will:

FILE IO AND DATA REPRSENTATION. Problem Solving with Computers-I

File Operations. Lecture 16 COP 3014 Spring April 18, 2018

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

We can even use the operator << to chain the output request as:

Engineering Problem Solving with C++, 3e Chapter 2 Test Bank

3.1. Chapter 3: Displaying a Prompt. Expressions and Interactivity

10/23/02 21:20:33 IO_Examples

3.1. Chapter 3: The cin Object. Expressions and Interactivity

The C++ Language. Arizona State University 1

Chapter Overview. I/O Streams as an Introduction to Objects and Classes. I/O Streams. Streams and Basic File I/O. Objects

Text File I/O. #include <iostream> #include <fstream> using namespace std; int main() {

Object Oriented Programming

Object-oriented Programming for Automation & Robotics Carsten Gutwenger LS 11 Algorithm Engineering

Chapter 12: Advanced File Operations

Transcription:

Streams - Object input and output in C++ Dr. Donald Davendra Ph.D. Department of Computing Science, FEI VSB-TU Ostrava Dr. Donald Davendra Ph.D. (Department of Computing Streams - Object Science, input FEI and VSB-TU output Ostrava) in C++ 1 / 35

Streams - Object input and output in C++ Basic concepts The character is an elementary text information, as given in the ASCII table - 8 bit code, character data types are char, string: - Printable characters from 32 to 127, - Control escape sequence, - Characters of national alphabets. 16-bit Unicode, character data types wchar t, string wstrng. Word - a sequence of characters. Words are divided from each other by punctuation marks and indenting. line of text - words ending sequence symbol (s) transition to a new line. Length of line? String class creates the necessary space. At the level of OS option to redirect input and output - programs that read from standard input and write to standard output are called filters. Dr. Donald Davendra Ph.D. (Department of Computing Streams - Object Science, input FEI and VSB-TU output Ostrava) in C++ 2 / 35

Text Input / Output: 1 To end the input from keyboard, Win32 ctrl-z, Unix ctrl-d. 2 Standard input and output reads in one line at a time. The program can read input until the moment you end the row by pressing enter. 3 iostream header. Dr. Donald Davendra Ph.D. (Department of Computing Streams - Object Science, input FEI and VSB-TU output Ostrava) in C++ 3 / 35

Classes for input and output stream Classes for input and output stream - text and binary data stream Header used is iostream, and the class is ios Classes for input and output are based on templates - the same interface for input and output text, regardless of its encoding. tool: classes and objects use the input current i, as input and output, whereas files streaming use f as file. Type char, Template: basic + class name: class stream description streambuf buffered ios input and output istream input ostream output fstream file ifstream input file ofstream output file Classes for standard I/O and file streams. Dr. Donald Davendra Ph.D. (Department of Computing Streams - Object Science, input FEI and VSB-TU output Ostrava) in C++ 4 / 35

Overloaded operators for input / output: 1 For output current (put to, insertion), 2 For input stream (get from, extraction). Objects standard text input and output Standard streams for C++ object-oriented I / O. description stream std. equipment cin std. input keyboard cout std. output screen cerr std. error output screen clog error output line by line screen Dr. Donald Davendra Ph.D. (Department of Computing Streams - Object Science, input FEI and VSB-TU output Ostrava) in C++ 5 / 35

Output formatting Operator << Left operand - an object of type ostream right operand - any type (for which the output is defined). cout << Ahoj! << e n d l ; i n t i = 5 ; double d = 1 2 3. 4 5 6 ; cout << i = << i << \ td= << d << e n d l ; Dr. Donald Davendra Ph.D. (Department of Computing Streams - Object Science, input FEI and VSB-TU output Ostrava) in C++ 6 / 35

Format flags for text streams name skipws left /right internal dec oct hex showbase showpoint uppercase showpos scientific fixed unitbuf stdio boolalpha adjustfield basefield floatfield description Sets the skipws format flag for the str stream. Sets the adjustfield format flag for the str stream to left/right. Sets the adjustfield format flag for the str stream to internal. Sets the basefield format flag for the str stream to dec. Sets the basefield format flag for the str stream to oct. Sets the basefield format flag for the str stream to hex. Sets the showbase format flag for the str stream. Sets the showpoint format flag for the str stream. Sets the uppercase format flag for the str stream. Sets the showpos format flag for the str stream. Sets the floatfield format flag for the str stream to scientific Sets the floatfield format flag for the str stream to fixed. Sets the unitbuf format flag for the str stream. used for stdout a stderr Sets the boolalpha format flag for the str stream. internal parameterl, left right dec, oct, hex fixed a scientific Dr. Donald Davendra Ph.D. (Department of Computing Streams - Object Science, input FEI and VSB-TU output Ostrava) in C++ 7 / 35

Formatting output using status flags formatting status flags defined in the ios class in the class namespace, hence eg ios :: hex. Enumerated data type for constants of format state flags fmtflags is defined in the ios class. You can combine bit or simply add - values to powers of two. methods setf () to set flags or unsetf () for their removal. cout object has access for other methods more than just setting and the status format flags. There are the particular methods put() and write(). Also these methods allows output to a text stream, which, however, unlike the overloaded operator <<, the value is not influenced by formatting symptoms. Dr. Donald Davendra Ph.D. (Department of Computing Streams - Object Science, input FEI and VSB-TU output Ostrava) in C++ 8 / 35

/ f i l e os t f l g. cpp / #i n c l u d e <i ostream > u s i n g namespace s t d ; i n t main ( ) { cout << 123 << \ t << 123.456 e15 << \ t << 0 x f e << \ t << t r u e << e n d l ; cout. s e t f ( i o s : : hex i o s : : showbase i o s : : b o o l cout. u n s e t f ( i o s : : dec ) ; // needed by some c o m p i l e r s cout << 123 << \ t << 123.456 e15 << \ t << 0 x f e << \ t << t r u e << e n d l ; Dr. Donald Davendra Ph.D. (Department of Computing Streams - Object Science, input FEI and VSB-TU output Ostrava) in C++ 9 / 35

123 1.23456e+017 254 1 0x7b 1.23456e+017 0xfe true Dr. Donald Davendra Ph.D. (Department of Computing Streams - Object Science, input FEI and VSB-TU output Ostrava) in C++ 10 / 35

Formatting output with manipulators handlers: 1 Special operators similar functions; 2 Used as its argument a reference to the stream, which also returned; 3 May be part of the command output. Dr. Donald Davendra Ph.D. (Department of Computing Streams - Object Science, input FEI and VSB-TU output Ostrava) in C++ 11 / 35

Formatting output with manipulators manipulator description property boolalpha log. values textually I/O dec decimal I/O endl newline O ends terminates line. and empties memory O fixed sets a fixed flag O flush empty the cache O hex hexadecimal I/O internal sets a flag O left decimal system I/O noboolalpha cancel flag I/O noshowbase cancel flag O noshowpoint cancel flag O noshowpos cancel flag O noskipws cancel flag I nounitbuf cancel flag O nouppercase cancel flag O oct octel I/O Dr. Donald Davendra Ph.D. (Department of Computing Streams - Object Science, input FEI and VSB-TU output Ostrava) in C++ 12 / 35

manipulator description property resetiosflags(fmtflags f) resets flags I/O right sets a flag O scientific sets a flag O setbase(int base) e.g (0, 8, 10, 16) I/O setfill(int ch) fills the characters O setiosflags(fmtflags f) sets the flags I/O setprecision(int p) output formatted to precision O setw(int w) width of the field O showbase sets a flag O showpoint sets a flag O showpos sets a flag O skipws sets a flag I unitbuf sets a flag O uppercase sets a flag uppercase O ws skip introductory whitespace I Dr. Donald Davendra Ph.D. (Department of Computing Streams - Object Science, input FEI and VSB-TU output Ostrava) in C++ 13 / 35

/ f i l e os tman. cpp / #i n c l u d e <i ostream > #i n c l u d e <iomanip> u s i n g namespace s t d ; i n t main ( ) { cout << s e t i o s f l a g s ( i o s : : showbase ) << hex << 123 << \ t << s e t p r e c i s i o n ( 3 ) << setw (10) << 123.456 e15 << \ t << oct << 0 x f e << e n d l ; r e t u r n 0 ; } // v o i d main ( ) 0x7b 1.23e+017 0376 Dr. Donald Davendra Ph.D. (Department of Computing Streams - Object Science, input FEI and VSB-TU output Ostrava) in C++ 14 / 35

Overloading the I/O operators for custom classes ostream & operator << (ostream & os, class id); istream & operator >> (istream & os, class & id); The need to transfer the reference to the stream and return the same reference is seemingly pointless. But only until you realize that calling the operator functions and finishing it definitely changes the status of the stream - either we wrote something in it, or we read something from it. Therefore, after we have outlined the activities of the operator s command to leave a status link for transferring the data stream. Writing object id in the stream does not require further attention. Reading an object id of the current requires attention - we mentioned object operator functions to load, so pass it by reference. Dr. Donald Davendra Ph.D. (Department of Computing Streams - Object Science, input FEI and VSB-TU output Ostrava) in C++ 15 / 35

/ s o u b o r s t r o c l s. cpp #i n c l u d e <cmath> #i n c l u d e <i o s t r e a m> #i n c l u d e <iomanip> using namespace std ; const double M PI = 3.1415926535897932384L ; c l a s s p o l a r { d o u b l e r, f i ; p u b l i c : p o l a r ( d o u b l e a, d o u b l e b ) { r = a ; f i = b ; } ; d o u b l e g e t r ( v o i d ) { r e t u r n r ; } ; d o u b l e g e t f i ( v o i d ) { r e t u r n f i ; } ; }; ostream& operator <<(ostream& os, polar& po ){ os << setw ( 8 ) << s e t p r e c i s i o n ( 4 ) << r = << po. g e t r ( ) << \ t f i = << po. g e t f i ( ) << e n d l ; r e t u r n os ; } Dr. Donald Davendra Ph.D. (Department of Computing Streams - Object Science, input FEI and VSB-TU output Ostrava) in C++ 16 / 35

// main ( ) v o i d main ( ) { p o l a r x ( 1. 0, M PI / 2. 0 ) ; cout << x ; // o p e r a t o r << ( cout, x ) } // v o i d main ( ) r =1 fi =1.571 Dr. Donald Davendra Ph.D. (Department of Computing Streams - Object Science, input FEI and VSB-TU output Ostrava) in C++ 17 / 35

Input from a text stream Input from a text stream is connected to the operator >>. The left operand must be an object of type istream, right operand can be of any type for which the input is defined. i n t i ; double d ; c i n >> i >> d ; i s t r e a m& o p e r a t o r >>(i s t r e a m& i s, p o l a r& po ){ r e t u r n i s ; } double a, b ; i s >> a >> b ; po = p o l a r ( a, b ) ; Dr. Donald Davendra Ph.D. (Department of Computing Streams - Object Science, input FEI and VSB-TU output Ostrava) in C++ 18 / 35

// s o u b o r s t r i c l s. cpp #i n c l u d e <cmath> #i n c l u d e <i o s t r e a m> #i n c l u d e <iomanip> using namespace std ; const double M PI = 3.1415926535897932384L ; c l a s s p o l a r { d o u b l e r, f i ; p u b l i c : p o l a r ( ) : r ( 0. 0 ), f i ( 0. 0 ) {}; p o l a r ( d o u b l e a ) : r ( a ) { f i = 0. 0 ; } ; p o l a r ( d o u b l e a, d o u b l e b ) : r ( a ), f i ( b ) {}; polar operator+ ( polar b ) ; d o u b l e g e t r ( v o i d ) { r e t u r n r ; } ; d o u b l e g e t f i ( v o i d ) { r e t u r n f i ; } ; }; Dr. Donald Davendra Ph.D. (Department of Computing Streams - Object Science, input FEI and VSB-TU output Ostrava) in C++ 19 / 35

ostream& operator <<(ostream& os, polar& po ){ os << setw ( 8 ) << s e t p r e c i s i o n ( 4 ) << r = << po. g e t r ( ) << \ t f i = << po. g e t f i ( ) << e n d l ; r e t u r n os ; } istream& operator >>(istream& is, polar& po ){ d o u b l e a, b ; i s >> a >> b ; po = p o l a r ( a, b ) ; r e t u r n i s ; } p o l a r p o l a r : : o p e r a t o r +( p o l a r b ){// o n l y I. and I I. q u a d r a n t d o u b l e abs, f i, u ; f i = t h i s >f i ( t h i s >f i b. f i ) / 2. 0 ; i f ( t h i s >f i < b. f i ) u = M PI + ( t h i s >f i b. f i ) ; e l s e u = M PI ( t h i s >f i b. f i ) ; abs = s q r t ( t h i s >r t h i s >r + b. r b. r 2. 0 t h i s >r b. r cos ( u ) ) ; r e t u r n p o l a r ( abs, f i ) ; } Dr. Donald Davendra Ph.D. (Department of Computing Streams - Object Science, input FEI and VSB-TU output Ostrava) in C++ 20 / 35

v o i d main ( ) { p o l a r x, y ( 1. 0, M PI / 2. 0 ) ; c i n >> x ; x = x + y ; cout << x ; // o p e r a t o r << ( cout, x ) } // v o i d main ( ) 1.5 r =1.72 fi =1.035 Dr. Donald Davendra Ph.D. (Department of Computing Streams - Object Science, input FEI and VSB-TU output Ostrava) in C++ 21 / 35

To enter additional methods are defined: for example, get() and read(). The last byte is loaded can be undone using the putback(). See the next incoming character without reading it can be accomplished using the peek() method. Note, that the current text can be input and output using the window buffer. Dr. Donald Davendra Ph.D. (Department of Computing Streams - Object Science, input FEI and VSB-TU output Ostrava) in C++ 22 / 35

// s o u b o r s t r i misc. cpp #i n c l u d e <i o s t r e a m> #i n c l u d e <iomanip> using namespace std ; i n t main ( ) { b o o l b ; cout << e n t e r t r u e o r f a l s e << e n d l ; cin >> boolalpha >> b ; cout << b << \ t << boolalpha << b << endl ; i n t d, o ; cout << e n t e r two o c t a l numbers << endl ; c i n >> o c t >> d >> o ; c h a r s [ 1 0 ] ; cout << e n t e r s t r i n g << e n d l ; c i n >> setw ( s i z e o f ( s ) ) >> s ; / / s w i l l not o v e r f l o w cout << endl << d = << d << \ to = << o << endl << s = << s << endl ; } // v o i d main ( ) Dr. Donald Davendra Ph.D. (Department of Computing Streams - Object Science, input FEI and VSB-TU output Ostrava) in C++ 23 / 35

enter true or false false 0 false enter two octal numbers 0123 0765 enter string Hello! d =83 o =501 s = Hello! Dr. Donald Davendra Ph.D. (Department of Computing Streams - Object Science, input FEI and VSB-TU output Ostrava) in C++ 24 / 35

Files The file is a sequence of characters (bytes) terminated with a special combination that has the contents of the file that are not the end of file, EOF symbolically. The text file contains lines of text. The binary file contains valuesin the same way as they are stored in computer memory. external file name - file name at the operating system; internal file name - identifying file in the program in C++, it most frequently the file called through he object, through which we work with the file. Each stream can be opened and closed. When you open the stream we provide connection between the internal and the external file name. When you open mode determine our approach to the data in the stream Closing the current stream enables the OS updates the directory information to the current state of the file that was associated with the stream. Dr. Donald Davendra Ph.D. (Department of Computing Streams - Object Science, input FEI and VSB-TU output Ostrava) in C++ 25 / 35

Object text input and output from / to files - Consistent with standard text input and output (in open streams): double f ; I n p u t c u r r e n t >> f ; Output c u r r e n t << has been l o a d e d : << f ; - uses the header #include <fstream> and the methods are: - ofstream for output - ifstream for input. Dr. Donald Davendra Ph.D. (Department of Computing Streams - Object Science, input FEI and VSB-TU output Ostrava) in C++ 26 / 35

opening a file-stream constructor: ifstream input current (file.in); ofstream output current (file.out); or open() close(). unsuccessful opening of current-null: i f ( i n p u t c u r r e n t == 0) c e r r << e r r o r opening i n p u t f i l e < f i l e. in > ; e l s e... Dr. Donald Davendra Ph.D. (Department of Computing Streams - Object Science, input FEI and VSB-TU output Ostrava) in C++ 27 / 35

Operating modes are set using ios:: mode description app connects the data is always written to the end of file ate opens and sets the end of file in when opening the set reading mode (default for ifstream) When out sets the mode when opening the notation (default for ofstream) binary opens the file in binary mode trunc cancel the contents of the file, if it exists working with a binary stream is an essential to use block transfer data. random access - Items have a binary file known size can calculate their position and to read or write to the specified position. put(), get() working with unformatted one byte (character), read(), write(): arguments address and number of bytes. Method eof() returns true when reaching the end of the file. Dr. Donald Davendra Ph.D. (Department of Computing Streams - Object Science, input FEI and VSB-TU output Ostrava) in C++ 28 / 35

// f i l e s t r o f b1. cpp // c r e a t e s f i l e odmoc. dta #i n c l u d e <f s t r e a m> #i n c l u d e <cmath> using namespace std ; const char name = odmoc. dta ; i n t main ( ) { o f s t r e a m o f s ( name, i o s : : out i o s : : b i n a r y ) ; i f ( o f s!= 0) { d o u b l e f ; f o r ( i n t i = 0 ; i < 1 0 0 ; i ++) { f = s q r t ( ( f l o a t ) i +1); o f s. w r i t e ( ( c o n s t c h a r ) &f, s i z e o f ( f ) ) ; } // f o r ( ; ; ) o f s. c l o s e ( ) ; } // i f ( o f s!= 0) r e t u r n 0 ; } // v o i d main ( ) Dr. Donald Davendra Ph.D. (Department of Computing Streams - Object Science, input FEI and VSB-TU output Ostrava) in C++ 29 / 35

+1.000 +1.414 +1.732 +2.000 +2.236 +2.449 +2.646 +2.828 +3.317 +3.464 +3.606 +3.742 +3.873 +4.000 +4.123 +4.243 +4.583 +4.690 +4.796 +4.899 +5.000 +5.099 +5.196 +5.292 +5.568 +5.657 +5.745 +5.831 +5.916 +6.000 +6.083 +6.164 +6.403 +6.481 +6.557 +6.633 +6.708 +6.782 +6.856 +6.928 +7.141 +7.211 +7.280 +7.348 +7.416 +7.483 +7.550 +7.616 +7.810 +7.874 +7.937 +8.000 +8.062 +8.124 +8.185 +8.246 +8.426 +8.485 +8.544 +8.602 +8.660 +8.718 +8.775 +8.832 +9.000 +9.055 +9.110 +9.165 +9.220 +9.274 +9.327 +9.381 +9.539 +9.592 +9.644 +9.695 +9.747 +9.798 +9.849 +9.899 Dr. Donald Davendra Ph.D. (Department of Computing Streams - Object Science, input FEI and VSB-TU output Ostrava) in C++ 30 / 35

// f i l e s t r i f bs. cpp #i n c l u d e <i o s t r e a m> #i n c l u d e <f s t r e a m> #i n c l u d e <iomanip> using namespace std ; const char name = odmoc. dta ; i n t main ( ) { i f s t r e a m i f s ( name, i o s : : i n i o s : : b i n a r y ) ; i f ( i f s!= 0) { d o u b l e f ; cout<< setw ( 7 ) << s e t p r e c i s i o n ( 4 ) <<s e t i o s f l a g s ( i o s : : s h o w p o i n t i o s : : showpos ) ; w h i l e ( t r u e ) { i f ( i f s. e o f ( ) ) { // t e s t o f f i l e end break ; } // i f ( i f s. e o f ( ) ) i f s. r e a d ( ( c h a r ) &f, s i z e o f ( f ) ) ; cout << f << \ t ; } // w h i l e ( t r u e ) i f s. c l o s e ( ) ; } // i f ( i f s!= 0) r e t u r n 0 ; } // v o i d main ( ) Dr. Donald Davendra Ph.D. (Department of Computing Streams - Object Science, input FEI and VSB-TU output Ostrava) in C++ 31 / 35

Open a file stream in binary mode can be read sequentially and randomly. It s the same with writing. For the combination of reading and writing over the same set - two indicators: get indicator - indicates the location in the file where the next read-pointer is; put indicator - the place, which will next write. Assistive interface methods described indicators programmer are called: tellg(), tellp() returns the value of the indicators seekg(), seekp() is set. pos type type for reading position in the file. Setting position: number of bytes and relative position. Dr. Donald Davendra Ph.D. (Department of Computing Streams - Object Science, input FEI and VSB-TU output Ostrava) in C++ 32 / 35

Constants also intended for reference position in the file, ios :: Identificator description beg shift to current position cur shift due early end shift due to the end ifs.seekg((i 1) sizeof(f), ios::beg); Dr. Donald Davendra Ph.D. (Department of Computing Streams - Object Science, input FEI and VSB-TU output Ostrava) in C++ 33 / 35

// s o u b o r s t r i f bn. cpp #i n c l u d e <i o s t r e a m> #i n c l u d e <f s t r e a m> #i n c l u d e <iomanip> using namespace std ; const char name = odmoc. dta ; i n t main ( ) { i f s t r e a m i f s ( name, i o s : : i n i o s : : b i n a r y ) ; i f ( i f s!= 0) { d o u b l e f ; i n t i ; cout<< setw ( 7 ) << s e t p r e c i s i o n ( 4 ) << s e t i o s f l a g s ( i o s : : s h owpoint i o s : : showpos ) ; cout << Enter number within range <1, 100>: ; c i n >> i ; w h i l e ( ( i > 0) && ( i < 1 0 1 ) ) { i f s. s e e k g ( ( i 1) s i z e o f ( f ), i o s : : beg ) ; i f s. r e a d ( ( c h a r ) &f, s i z e o f ( f ) ) ; cout << square root of << i << \ t i s << f << e n d l ; cout << Enter number within range <1, 100>: ; c i n >> i ; } // w h i l e ( ) cout << endl << End. << endl ; i f s. c l o s e ( ) ; } // i f ( i f s!= 0) r e t u r n 0 ; } // v o i d main ( ) Dr. Donald Davendra Ph.D. (Department of Computing Streams - Object Science, input FEI and VSB-TU output Ostrava) in C++ 34 / 35

Enter number within range <1, 100>: 2 square root of +2 is +1.414 Enter number within range <1, 100>: 4 square root of +4 is +2.000 Enter number within range <1, 100>: 29 square root of +29 is +5.385 Enter number within range <1, 100>: 0 End. Dr. Donald Davendra Ph.D. (Department of Computing Streams - Object Science, input FEI and VSB-TU output Ostrava) in C++ 35 / 35