Download presentation
Presentation is loading. Please wait.
Published byBlaze Gilbert Modified over 9 years ago
1
Dale Roberts 1 Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI E-mail: droberts@cs.iupui.edu Input/Output Streams File IO
2
Dale Roberts 2 File Streams Provides high level support for file operations in C++ Consists of three components: ifstream - allows only input ifstream - allows only input ofstream - allows only output ofstream - allows only output fstream - allows both input and output fstream - allows both input and output
3
Dale Roberts 3 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
4
Dale Roberts 4 Class Hierarchies ios istreamostream iostream fstreambase ifstreamofstream fstream
5
Dale Roberts 5 File Stream Functions Contains the following functions: open() - opens the stream open() - opens the stream close() - closes the stream close() - closes the stream attach() - attaches the stream to file descriptor attach() - attaches the stream to file descriptor setbuf() - set the stream buffer setbuf() - set the stream buffer rdbuf() - returns the pointer to stream buffer rdbuf() - returns the pointer to stream buffer str() - returns a pointer to the buffer array str() - returns a pointer to the buffer array
6
Dale Roberts 6 File Streams - Syntax Syntaxfstream(); fstream(const char *sFileName, int nMode, int nProt = 0664); fstream(filedesc fdif);
7
Dale Roberts 7 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
8
Dale Roberts 8 File Protection Modes nProt can be one of the following: filbuf::shcompat - compatibility share mode filbuf::shcompat - compatibility share mode filebuf::sh_none - no sharing filebuf::sh_none - no sharing filebuf::sh_read - read sharing filebuf::sh_read - read sharing filebuf::sh_write - write sharing filebuf::sh_write - write sharing filebuf::sh_read|filebuf::sh_write - both read and write sharing filebuf::sh_read|filebuf::sh_write - both read and write sharing
9
Dale Roberts 9 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
Dale Roberts 10 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);
11
Dale Roberts 11 File Streams - close() Close the opened file. The stream’s error flag is cleared unless the close fails. Syntax void close() Usagemy_file.close();
12
Dale Roberts 12 File I/O - Example #include #include #include #include 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.in for reading fIn.open("copy.in", ios::in); //Open the file copy.out for writing fOut.open("copy.out", ios::out); //Open the file copy.out for writing fOut.open("copy.out", ios::out);
13
Dale Roberts 13 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; } //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; //Write the final byte count cout << "[ " << nCnt << " ]" << endl; fOut << nCnt; //Close the files fIn.close(); fOut.close(); //Close the files fIn.close(); fOut.close(); return 0;} return 0;}
14
Dale Roberts 14 File I/O - Files copy.in abcd efg hi j copy.out abcd efg hi j 14
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.