Files in C++
Files and Stream classes Files the collection of records or programs of same nature. In C++, the concept of stream or stream classes to implement the I/O operations with the console and disk files. The I/O devices such as terminals, keyboards, disk etc. supplies an interface to the programmer for the smooth working with the system. This interface is known as stream. A stream is a sequence of bytes. It acts either as a source from which the input data can be obtained or a destination to which the output data can be sent. The source stream that provides data to the program is called the input stream and the destination stream that receives output from the program is known as output stream. cin and cout are used earlier in programs to perform this input and output operations
Files and Stream classes ios ostream streambuf istream iostream istream_withassign Iostream_withassign ostream_withassign Input Pointer output
Stream classes for console operations Class Name Contents ios Contains all basic facilities of input and output classes istream get(), getline(), read() and operator such as >> ostream put() and write(), insertion operator << iostream All input and output functions(ios/istream/ostream) streambuf It acts as a base for filebuf class used in files
Unformatted I/O operations cin>>var1>>var2>>……>>varn; cout<<var1<<var2<<……<<varn; Eg: put() and get() char c; cin.get(c); char c; cin.get(c); cout.put(c);
Unformatted I/O operations getline() and write() The getline() reads a whole line of text end with newline character(or when we press return(enter) key) cin.getline(line, size); Eg; char name[20]; cin.getline(name, 20); cout.write(line, size); The write() displays/prints a whole line of text
Classes for file stream operations ios ostream streambuf istream iostream ifstream fstream ofstream Input Pointer output filebuf fstream base iostream.h file fstream.h file
Details of file stream classes Class Contents filebuf Set the file buffers to read and write. It contains close() and open() as members fstreambase Provides operations common to the file streams, serves the base for ifsteram, ofstream, fstream classes. It contains open() and close() functions ifstream Provide input operations. Contains open() as default input mode. Inherits the functions get(), getline(), read(), seekg() and tellg() ofstream Provide output operations. Contains close() as default output mode. Inherits the functions put(), write(), seekp() and tellp() fstream Provides all input and output operations. It performs all functions of ifstream and ofstream. Inherits all istream and ostream classes through iostream
Opening and closing a file • Suitable name for the file • Data type structure • Purpose • Opening method • Input.data • Test.doc • Student • output
Opening and closing a file • Opening method 1. Using the constructor function of the class Here constructors are used in files to initialize the file stream object. 1. Choose appropriate filestream object 2. Initialize the object using suitable file name ofstream outfile(“results”); // this creates outfile as an ofstream object that manages the output stream This statement also opens the file results and attaches it to the output stream outfile Ifstream infile(“input.dat”); outfile<<“Total”; outfile<<sum; infile>> rollno; outfile.close(); //to close the outfile infile.close();
Opening and closing a file • Opening method 1. Using the open() function for opening the file The function open() can be used to open multiple files that use the same stream object. file_stream_class stream_object; Stream_object.open(“filename”); ofstream outfile; outfile.open(“Data1”); …………………….. outfile.close(); outfile.open(“Data2”); …………………………… outfile.close();
File Opening modes • Opening modes • It specifies the purpose of opening the file stream_object.open(“file_name”, mode); Mode specifies the purpose for which the file is opened. Default mode for ifstream is ios::in and for ofstream is ios::out Parameter Meaning ios::app Append to end-of-file ios::ate Go to the end-of-file on opening ios::binary Binary file ios::in Open file for reading only ios::nocreate Open fails if the file does not exists ios::noreplace Open fails if the file exists ios::out Open files for writing only ios::trunc Delete contents of the file if it exists
File pointers and their manipulations Each file has two associated pointers known as file pointers. One is the input pointer (get pointer) and other one is output pointer (put pointer) We are using it for reading and writing the files seekg() Moves get pointer to a specified position seekp() Moves the put pointer to a specified position tellg() Gives the current position of the get pointer tellp() Gives the current position of the put pointer infile.seekg(10);
File pointers and their manipulations Specifying the offset seekg(offset, refposition); seekp(offset, refposition); The parameter offset represents the number of bytes the file pointer is to be moved from the location specified by the parameter refposition. Refpositions can from the following ios::beg start of the file ios::cur current position of the pointer ios::end end of the file fout.seekg(0, ios::beg) go to the start fout.seekg(0, ios::cur) stay at the current position fout.seekg(0, ios::end) go to the end of the file fout.seekg(m, ios::beg) go to the m+1th postion fout.seekg(-m, ios::cur) go backwards by m bytes from the current position

Files in C++.pdf is the notes of cpp for reference

  • 1.
  • 2.
    Files and Streamclasses Files the collection of records or programs of same nature. In C++, the concept of stream or stream classes to implement the I/O operations with the console and disk files. The I/O devices such as terminals, keyboards, disk etc. supplies an interface to the programmer for the smooth working with the system. This interface is known as stream. A stream is a sequence of bytes. It acts either as a source from which the input data can be obtained or a destination to which the output data can be sent. The source stream that provides data to the program is called the input stream and the destination stream that receives output from the program is known as output stream. cin and cout are used earlier in programs to perform this input and output operations
  • 3.
    Files and Streamclasses ios ostream streambuf istream iostream istream_withassign Iostream_withassign ostream_withassign Input Pointer output
  • 4.
    Stream classes forconsole operations Class Name Contents ios Contains all basic facilities of input and output classes istream get(), getline(), read() and operator such as >> ostream put() and write(), insertion operator << iostream All input and output functions(ios/istream/ostream) streambuf It acts as a base for filebuf class used in files
  • 5.
  • 6.
    Unformatted I/O operations getline()and write() The getline() reads a whole line of text end with newline character(or when we press return(enter) key) cin.getline(line, size); Eg; char name[20]; cin.getline(name, 20); cout.write(line, size); The write() displays/prints a whole line of text
  • 7.
    Classes for filestream operations ios ostream streambuf istream iostream ifstream fstream ofstream Input Pointer output filebuf fstream base iostream.h file fstream.h file
  • 8.
    Details of filestream classes Class Contents filebuf Set the file buffers to read and write. It contains close() and open() as members fstreambase Provides operations common to the file streams, serves the base for ifsteram, ofstream, fstream classes. It contains open() and close() functions ifstream Provide input operations. Contains open() as default input mode. Inherits the functions get(), getline(), read(), seekg() and tellg() ofstream Provide output operations. Contains close() as default output mode. Inherits the functions put(), write(), seekp() and tellp() fstream Provides all input and output operations. It performs all functions of ifstream and ofstream. Inherits all istream and ostream classes through iostream
  • 9.
    Opening and closinga file • Suitable name for the file • Data type structure • Purpose • Opening method • Input.data • Test.doc • Student • output
  • 10.
    Opening and closinga file • Opening method 1. Using the constructor function of the class Here constructors are used in files to initialize the file stream object. 1. Choose appropriate filestream object 2. Initialize the object using suitable file name ofstream outfile(“results”); // this creates outfile as an ofstream object that manages the output stream This statement also opens the file results and attaches it to the output stream outfile Ifstream infile(“input.dat”); outfile<<“Total”; outfile<<sum; infile>> rollno; outfile.close(); //to close the outfile infile.close();
  • 11.
    Opening and closinga file • Opening method 1. Using the open() function for opening the file The function open() can be used to open multiple files that use the same stream object. file_stream_class stream_object; Stream_object.open(“filename”); ofstream outfile; outfile.open(“Data1”); …………………….. outfile.close(); outfile.open(“Data2”); …………………………… outfile.close();
  • 12.
    File Opening modes •Opening modes • It specifies the purpose of opening the file stream_object.open(“file_name”, mode); Mode specifies the purpose for which the file is opened. Default mode for ifstream is ios::in and for ofstream is ios::out Parameter Meaning ios::app Append to end-of-file ios::ate Go to the end-of-file on opening ios::binary Binary file ios::in Open file for reading only ios::nocreate Open fails if the file does not exists ios::noreplace Open fails if the file exists ios::out Open files for writing only ios::trunc Delete contents of the file if it exists
  • 13.
    File pointers andtheir manipulations Each file has two associated pointers known as file pointers. One is the input pointer (get pointer) and other one is output pointer (put pointer) We are using it for reading and writing the files seekg() Moves get pointer to a specified position seekp() Moves the put pointer to a specified position tellg() Gives the current position of the get pointer tellp() Gives the current position of the put pointer infile.seekg(10);
  • 14.
    File pointers andtheir manipulations Specifying the offset seekg(offset, refposition); seekp(offset, refposition); The parameter offset represents the number of bytes the file pointer is to be moved from the location specified by the parameter refposition. Refpositions can from the following ios::beg start of the file ios::cur current position of the pointer ios::end end of the file fout.seekg(0, ios::beg) go to the start fout.seekg(0, ios::cur) stay at the current position fout.seekg(0, ios::end) go to the end of the file fout.seekg(m, ios::beg) go to the m+1th postion fout.seekg(-m, ios::cur) go backwards by m bytes from the current position