Presentation is loading. Please wait.

Presentation is loading. Please wait.

Input/Output Streams, Part 2

Similar presentations


Presentation on theme: "Input/Output Streams, Part 2"— Presentation transcript:

1 Input/Output Streams, Part 2
Department of Computer and Information Science, School of Science, IUPUI Input/Output Streams, Part 2 Dale Roberts, Lecturer Computer Science, IUPUI 10/6/2019

2 Stream Manipulators C++ provides various stream manipulators that perform formatting tasks: setting widths, precision, filling, flushing streams, skipping white spaces, inserting new line, inserting a null character, etc.….. 10/6/2019

3 Integral Stream Base Integers are normally represented as base 10 values. To change the base in which integers are interpreted on a stream, insert different manipulators – hex, oct – to reset the stream to decimal insert dec. The base of a stream may also be changed by the stream manipulator setbase, which takes one integer argument of 10, 8 or 16 to set the base. As setbase takes an argument, it is called a parameterized stream manipulator – requires the inclusion of <iomanip.h> file 10/6/2019

4 Floating Point Precision
Controlling of the floating point precision (i.e., number of digits to the right side of the decimal point) is done by setprecision (manipulator) or precision (member function). A call to these, sets the precision for all subsequent output operations until the next precision-setting call is made. A call to precision without arguments returns the current precision setting. 10/6/2019

5 Field Width The ios width member function sets the field width (i.e., the number of character positions in which a value should be output or the number of characters that should be input) and returns the previous width. If values processed are smaller than the field width, fill characters are inserted as padding. A value wider than the designated width will not be truncated – the full number is printed. 10/6/2019

6 Other Manipulators User-defined
Stream Format State Flags (specifies the kind of formatting to be performed during stream I/O operations) trailing zeros, justification, padding, stream base, scientific notation, upper/lower cases, setting/resetting format flags, etc. 10/6/2019

7 File Streams Provides high level support for file operations in C++
Consists of three components: fstream - allows both input and output ifstream - allows only input ofstream - allows only output 10/6/2019

8 Files -- fstream class Inherits from fstreambase and iostream classes
fstreambase inherits from ios class iostream – inherits from istream and ostream classes ifstream – inherits from fstreambase and istream classes ofstream – inherits from fstreambase and ostream classes 10/6/2019

9 File Stream Functions Contains the following functions:
open() - opens the stream close() - closes the stream attach() - attaches the stream to file descriptor setbuf() - set the stream buffer rdbuf() - returns the pointer to stream buffer str() - returns a pointer to the buffer array 10/6/2019

10 File Streams - Syntax Syntax fstream();
fstream(const char *sFileName, int nMode, int nProt = 0664); fstream(filedesc fdif); 10/6/2019

11 File Processing Modes nMode can be any of the following:
ios::in - input processing ios::out - output processing ios::trunc - discard the file contents ios::nocreate - the file must exist in order to write ios::noreplace - cannot rewrite to an existing file ios::binay - open the file in binary mode ios::app - append the new data to the contents of the file ios::ate - open the file for output and move to the end ios::in|ios::out - both input and output 10/6/2019

12 File Protection Modes nProt can be one of the following:
filbuf::shcompat - compatibility share mode filebuf::sh_none - no sharing filebuf::sh_read - read sharing filebuf::sh_write - write sharing filebuf::sh_read|filebuf::sh_write - both read and write sharing 10/6/2019

13 File Streams - Example fstream my_file;
fstream myfile(“cs265.txt”, ios::in) fstream myfile(“cs265.txt”, ios::out) const int nMAX_SIZE = 256; char buf[nMAX_SIZE]; filedesc fdesc = my_file.fd(); fstream fs(fdesc); 10/6/2019

14 File Streams - open() Opens a file as a stream Syntax
void open(const char *sfile, int nMode, int nProt = 0664); Usage fstream my_file; my_file.open(“cs265.txt”, ios::out); 10/6/2019

15 File Streams - close() Close the opened file.
The stream’s error flag is cleared unless the close fails. Syntax void close() Usage my_file.close(); 10/6/2019

16 File I/O - Example #include<iostream.h> #include<fstream.h> main() { // fIn and fOut are objects of class fstream fstream fIn, fOut; int nCnt = 0; //for byte count; char cCh; //Open the file copy.in for reading fIn.open("copy.in", ios::in); //Open the file copy.out for writing fOut.open("copy.out", ios::out); 10/6/2019

17 File I/O – Example contd…
//Until the end of file is reached, read from //the file and write it to the out file and echo on the screen. while(fIn.get(cCh)) { fOut.put(cCh); cout.put(cCh); //echo on terminal ++nCnt; } //Write the final byte count cout << "[ " << nCnt << " ]" << endl; fOut << nCnt; //Close the files fIn.close(); fOut.close(); return 0;} 10/6/2019

18 File I/O - Files copy.in abcd efg hi j copy.out abcd efg hi j 14
10/6/2019


Download ppt "Input/Output Streams, Part 2"

Similar presentations


Ads by Google