{"id":209,"date":"2018-07-11T07:27:19","date_gmt":"2018-07-11T07:27:19","guid":{"rendered":"http:\/\/itp1.epgpbooks.inflibnet.ac.in\/?post_type=chapter&#038;p=209"},"modified":"2018-07-30T09:34:41","modified_gmt":"2018-07-30T09:34:41","slug":"working-with-files-in-c","status":"publish","type":"chapter","link":"https:\/\/ebooks.inflibnet.ac.in\/itp1\/chapter\/working-with-files-in-c\/","title":{"rendered":"Working with Files in C++"},"content":{"raw":"<div>\r\n\r\n<strong>Introduction<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">So far, we have used predefined standard I\/O streams cin and cout to read input from standard input device and write output to standard output device. Here data storage is transient. We will now extend this concept to disk files.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">Files are used for persistent data storage. Most of the applications require storing data in files and use it later. Files usually resides on secondary storage devices like disk. In this module, we will see input\/output operations using file streams.<\/p>\r\n&nbsp;\r\n\r\n<strong>1.\u00a0\u00a0\u00a0\u00a0\u00a0 File<\/strong>\r\n\r\n&nbsp;\r\n\r\nFile is used for persistent data storage. It resides on non- volatile secondary storage devices to retain the data for longer time. Data in files gets lost only when someone erases the data or when hardware storage media fails due to some reasons. Each file has End-Of-File (EOF) marker and it varies as per operating system. Actually all files are managed by operating system.\r\n\r\n&nbsp;\r\n\r\n<strong>2.\u00a0 Need of file<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Terminal I\/O using streams cin and cout stores the data in iostreams temporarily. Data in these streams are lost when the execution of an application gets terminated or when power fails. To have the data available even after program termination, it should be stored in files on non-volatile memory devices like disks. In most of the applications, the data needs to be retained for later use. Data may be created by one program and used by many programs. For example, name and registration number of students may be used by applications in accounts department, exam department. In railway reservation system, it requires to store the details about trains, schedule, fare, ticket booking etc. in persistent storage.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">Sometimes when data size is large, one may like to create data in file and then use it. For example, giving input for a small matrix of size 10 x 10 requires 100 elements to be entered. A small mistake while entering data may require repeating entry of all elements. Here, it is better to create one text file and enter 100 elements in it. These data can be read by program to perform matrix operation. It will help to reduce data entry time and the chances of errors.<\/p>\r\n&nbsp;\r\n\r\n<strong>3.\u00a0 File Streams<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Working with files requires the use of file-oriented classes: ifstream for input, ofstream for output and fstream for both input and output. Class ifstream (input file stream) is derived from istream, ofstream (output file stream) is derived from ostream and fstream (file stream) is derived from iostream. The ancestor classes (istream, ostream and iostream) are in turn derived from class ios. The file-oriented classes are also derived from the fstreambase class with multiple inheritances. This fstreambase class contains an object of class filebuf, providing a file-oriented buffer and its associated member functions. Class filebuf is derived from the more general streambufclass.<span style=\"text-align: initial;font-size: 1em\">Classesifstream, <\/span>ofstream and fstream<span style=\"text-align: initial;font-size: 1em\"> are declared in the <\/span>fstream<span style=\"text-align: initial;font-size: 1em\"> file. Thus, to perform file processing in C++, header files &lt;iostream&gt; and &lt;fstream&gt; must be included in C++ source file.<\/span>ofstreamobject<span style=\"text-align: initial;font-size: 1em\"> represents the output file stream and is used to create <\/span>filesand<span style=\"text-align: initial;font-size: 1em\">\/or write information to files.\u00a0<\/span>ifstream<span style=\"text-align: initial;font-size: 1em\"> object represents the input file stream and is used to <\/span>readinformation<span style=\"text-align: initial;font-size: 1em\"> from files.<\/span>fstream<span style=\"text-align: initial;font-size: 1em\"> object represents the file stream having <\/span>capabilitiesof<span style=\"text-align: initial;font-size: 1em\"> both <\/span>ofstream<span style=\"text-align: initial;font-size: 1em\"> and <\/span>ifstream<span style=\"text-align: initial;font-size: 1em\"> which means it can create files, <\/span>writeinformation<span style=\"text-align: initial;font-size: 1em\"> to files, and read information from files.<\/span><\/p>\r\n\r\n<\/div>\r\n<div>\r\n\r\n&nbsp;\r\n\r\n<strong>4.\u00a0\u00a0\u00a0\u00a0\u00a0 Differences between a predefined standard stream and a file stream<\/strong>\r\n\r\n<strong>\u00a0<\/strong>\r\n\r\n\u00b7\u00a0\u00a0\u00a0\u00a0\u00a0 A file stream needs to be created and connected to a file before it can be used. The predefined streams are available to all programs and can be used right away.\r\n\r\n\u00b7\u00a0\u00a0\u00a0\u00a0\u00a0 Even though read\/write can be performed from any position with basic iostreams; this usually does not make any sense with the predefined streams, as they are connected to the sequential devices by default. One can reposition a file stream to arbitrary file positions and access data randomly also.\r\n\r\n&nbsp;\r\n\r\n<strong>5.\u00a0\u00a0\u00a0\u00a0\u00a0 File operations<\/strong>\r\n\r\n&nbsp;\r\n\r\nWhen working with files, it requires file operations like connecting the physical file with file stream, opening the file, reading\/writing, checking errors and closing the file.\r\n\r\n\/\/\u00a0 basic file operations #include &lt;iostream&gt; #include &lt;fstream&gt; usingnamespacestd;\r\n\r\nint main ()\r\n\r\n&nbsp;\r\n\r\n{\r\n\r\nofstreamoutfile;\r\n\r\noutfile.open (\"example.txt\");\r\n\r\noutfile&lt;&lt; \"This is my first attempt to use a file\\n\";\r\n\r\noutfile.close();\r\n\r\nreturn 0;\r\n\r\n}\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">As file stream classes are derived from i\/o stream classes, it is easy to read\/write from files. All the members of base classes are available to file classes. All of the member functions and operators that one can apply to istream or ostream or iostream object can also be applied to ifstream and ofstream and fstream objects. Thus reading and writing operations using file stream objects are very similar to reading\/writing using standard streams cin and cout.As an extension of the base classes, file streams have some additional member functions and internal information.Internally file objects use a special kind of stream buffer, called a file buffer, to control transferring characters to\/from the associated file.<\/p>\r\n&nbsp;\r\n\r\n<strong>5.1.\u00a0\u00a0\u00a0 Opening a file<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Before performing input\/output operations on file, it requires a file to be opened. When opening a file, it requires supplying the physical file name and its location. The operating system (OS) searches its file system for the file of that name or create file with specified name.<\/p>\r\n&nbsp;\r\n\r\nThe OS will look for a file with specified name in thesame directory as of executable C++ files or project. If the file is placedin other directory, it should be specified with exact path.\r\n\r\n<\/div>\r\n<div>\r\n\r\nA file can be opened in two ways:\r\n\r\n&nbsp;\r\n\r\n1.\u00a0\u00a0\u00a0\u00a0 Using parameterized constructor - Open a file on construction of a file stream\r\n\r\n2.\u00a0\u00a0\u00a0\u00a0 Using open () function to connect a file with existing file stream\r\n\r\n&nbsp;\r\n\r\n<strong>5.1.1. Open a file on <\/strong>construction<strong> of a file stream using parameterized constructor<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">While creating file stream object, specify a file name with file path as argument. It performs both the tasks of creating a file stream and connecting it to a specified file in a single statement.If an operation fails due to some reason, file stream is not ready for use.<\/p>\r\n&nbsp;\r\n\r\nExample:\r\n<p style=\"text-align: justify\">ifstreaminfile (\u201cinp.dat\u201d); \/\/create file stream infile, connect to file named \u2018inp.dat\u2019 if (!infile) \/\/ check error: unable to open file for input File will be closed automatically on destruction of the stream object.<\/p>\r\n&nbsp;\r\n\r\n<strong>5.1.2. Using open () function to connect a file with existing file stream<\/strong>\r\n\r\n&nbsp;\r\n\r\nHere, it requires two statements: first to create file stream and second to connect the file.\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">1.\u00a0\u00a0\u00a0\u00a0\u00a0 Create anempty file stream using constructor without parameter. Here, a file stream is created that is not connected to any file. Any operation on the file stream will fail.<\/p>\r\n<p style=\"text-align: justify\">2.\u00a0\u00a0\u00a0\u00a0\u00a0 When needed, invoke open() function with file name with file path as parameter to connect it to the file stream.If the specified file cannot be opened due to some reason, failbit will be set; otherwise file stream is ready for use.<\/p>\r\n&nbsp;\r\n\r\nExample:\r\n\r\n&nbsp;\r\n\r\nifstreaminfile; \\\\ 1- file stream infile is created, but not connected to any file \u2026\r\n\r\ninfile.open(\u201cinp.dat\u201d); \\\\ 2 - file \u2018inp.dat\u2019 is opened and connected to file stream infile if (!infile) \/\/ error: unable to open file for input\r\n\r\n&nbsp;\r\n\r\nAdvantages of using this method of creating file first and connecting later are as follows:\r\n\r\n&nbsp;\r\n\r\n\u00b7\u00a0\u00a0\u00a0\u00a0\u00a0 Once a stream is created, it can be used to connect to any file later using open().\r\n\r\n\u00b7\u00a0\u00a0\u00a0\u00a0\u00a0 It enables the programmer to bind a file stream to more than one file during the lifetime of a stream.\r\n\r\n\u00b7\u00a0\u00a0\u00a0\u00a0\u00a0 Closing the file does not destroy the stream. It simply gets disconnected from the physical file. Stream is still available for another physical file to get connected.\r\n\r\n&nbsp;\r\n\r\n<strong>5.1.3. File open mode<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Files are opened using default mode when modeis not specified. It defaults to text file with input mode for ifstream; output mode for ofstream and input as well as output modes for fstream. Sometimes, it may be required to change this default. For example, one may want to have binary I\/O; or want to append data in existing file. For this, C++ provides various modes that can be used while opening the file. Open mode is defined in ios_base class.The ios_base::openmode is of a bitmask type like the format flags and the stream state. The bits and the meaning of open mode are as follows:<\/p>\r\n&nbsp;\r\n<table class=\"aligncenter\" border=\"1\">\r\n<tbody>\r\n<tr>\r\n<td style=\"width: 153.063px\">Open mode bit<\/td>\r\n<td style=\"width: 507.063px\">Meaning<\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 153.063px\">ios_base::in<\/td>\r\n<td style=\"width: 507.063px\">Open file for reading. File must exist. Default for<\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 153.063px\"><\/td>\r\n<td style=\"width: 507.063px\">ifstream.<\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 153.063px\">ios_base::out<\/td>\r\n<td style=\"width: 507.063px\">Open file for writing. If file exists, existing file is<\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 153.063px\"><\/td>\r\n<td style=\"width: 507.063px\">deleted and a new empty file is created with same<\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 153.063px\"><\/td>\r\n<td style=\"width: 507.063px\">name. If file does not exist, it will create a new<\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n<\/div>\r\n<div>\r\n<table class=\"aligncenter\" border=\"1\">\r\n<tbody>\r\n<tr>\r\n<td><\/td>\r\n<td>file. Default for ofstream.<\/td>\r\n<\/tr>\r\n<tr>\r\n<td>ios_base::ate<\/td>\r\n<td>Start position for reading or writing is at end of<\/td>\r\n<\/tr>\r\n<tr>\r\n<td><\/td>\r\n<td>file (AT End).<\/td>\r\n<\/tr>\r\n<tr>\r\n<td>ios_base::app<\/td>\r\n<td>Start\u00a0 position for\u00a0 writing is\u00a0 at\u00a0 end of\u00a0 file<\/td>\r\n<\/tr>\r\n<tr>\r\n<td><\/td>\r\n<td>(APPend)<\/td>\r\n<\/tr>\r\n<tr>\r\n<td>ios_base::trunk<\/td>\r\n<td>Truncate\u00a0 file\u00a0 to\u00a0 zero\u00a0 length\u00a0 if\u00a0 it\u00a0 exists<\/td>\r\n<\/tr>\r\n<tr>\r\n<td><\/td>\r\n<td>(TRUNCate)<\/td>\r\n<\/tr>\r\n<tr>\r\n<td>ios_base::binary<\/td>\r\n<td>Open file in Binary mode for binary I\/O<\/td>\r\n<\/tr>\r\n<tr>\r\n<td>ios_base::nocreate<\/td>\r\n<td>Do not allow to create a new file. When opening,<\/td>\r\n<\/tr>\r\n<tr>\r\n<td><\/td>\r\n<td>file must exist. If the file to be opened does not<\/td>\r\n<\/tr>\r\n<tr>\r\n<td><\/td>\r\n<td>exist, an error will occur.<\/td>\r\n<\/tr>\r\n<tr>\r\n<td>ios_base::noreplace<\/td>\r\n<td>Do not allow to replace the content of file. Error<\/td>\r\n<\/tr>\r\n<tr>\r\n<td><\/td>\r\n<td>will occur when opening file for output if file<\/td>\r\n<\/tr>\r\n<tr>\r\n<td><\/td>\r\n<td>already exists, unless ate or app is set.<\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n&nbsp;\r\n\r\nDefault file open mode is as follows:\r\n<table class=\"aligncenter\" border=\"1\">\r\n<tbody>\r\n<tr>\r\n<td style=\"width: 251.063px\">File Stream<\/td>\r\n<td style=\"width: 409.063px\">Default Open Mode<\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 251.063px\">ifstream<\/td>\r\n<td style=\"width: 409.063px\">in<\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 251.063px\">ofstream<\/td>\r\n<td style=\"width: 409.063px\">out|trunc<\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 251.063px\">fstream<\/td>\r\n<td style=\"width: 409.063px\">in|out|trunc<\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n&nbsp;\r\n<p style=\"text-align: justify\">File open mode flag bits can be combined for effects of multiple flags using OR(|). For example, ios::out|ios::app\u00a0 Open file for writing by appending new content. It creates file if it does not exist. If file already exists, file pointer is positioned at the end.<\/p>\r\nios::in|ios::out\r\n\r\nOpen file for reading as well as writing. File must exist.\r\n\r\nios::in|ios::out | ios::trunc\r\n\r\n&nbsp;\r\n\r\nOpen file for reading as well as writing. It empties fileif it exists, creates otherwise.\r\n\r\n&nbsp;\r\n\r\nWhile opening the file, file name and open mode can be passed as two parameters in constructor as well as open() function.\r\n\r\n&nbsp;\r\n\r\n\u00b7\u00a0\u00a0\u00a0\u00a0\u00a0 Constructor: &lt;file stream&gt; (const char *filename, ios_base::openmode mode);\r\n\r\n\u00b7\u00a0\u00a0\u00a0\u00a0\u00a0 Function open(): void open(const char *filename, ios_base::openmode mode);\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">In the given examples, constructor and open() are used with file name only as an argument. In absence of second argument, default value of file open mode is considered depending upon the type of file stream object.One can explicitly specify the second argument for open mode in the file stream constructor while creating file stream object or in open() call.<\/p>\r\n&nbsp;\r\n\r\nFor example,\r\n\r\n&nbsp;\r\n\r\nofstreamfout(\u201cout.txt\u201d,ios_base::out|ios_base::app);\r\n\r\nofstreamoutfile;\r\n\r\noutfile.open(\u201cstudents.txt\u201d, ios::out|ios::app);\r\n\r\nNote that class ios is derived from ios_base, so all the members of ios_base class are inherited in ios class.\r\n\r\n<\/div>\r\n<div>\r\n\r\n5.1.4. Various ways to test whether a file is opened successfully or not\r\n\r\n&nbsp;\r\n\r\nTo test whether the file is successfully opened or not, one way is to use is_open() method. One may even check error state bits and use methods good(), bad(), fail(), rdstate() etc. as described with standard streams.\r\n<ul>\r\n \t<li>\u00b7\u00a0\u00a0\u00a0\u00a0\u00a0 Function is_open() returns true if open is successful<\/li>\r\n \t<li>\u00b7\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Functions good()returns true if open is successful, bad() and fail() returns true when unsuccessful<\/li>\r\n \t<li>\u00b7\u00a0\u00a0\u00a0\u00a0\u00a0 Operator !returns true when open is unsuccessful<\/li>\r\n \t<li>\u00b7\u00a0\u00a0\u00a0\u00a0\u00a0 Stream pointer is null when open is unsuccessful<\/li>\r\n<\/ul>\r\nExamples:\r\n\r\n&nbsp;\r\n\r\nfstreamfobj; fobj.open(\u201cdata.txt\u201d, ios::in | ios::out);\r\n<ul>\r\n \t<li>\u00b7\u00a0\u00a0\u00a0\u00a0\u00a0 if (fobj.is_open()) {\/\/process} else {\/\/error}<\/li>\r\n \t<li>\u00b7\u00a0\u00a0\u00a0\u00a0\u00a0 if (fobj.good()) {\/\/process} else {\/\/error}<\/li>\r\n \t<li>\u00b7\u00a0\u00a0\u00a0\u00a0\u00a0 If (fobj.fail()) {\/\/error} else {\/\/process}<\/li>\r\n \t<li>\u00b7\u00a0\u00a0\u00a0\u00a0\u00a0 If (fobj.bad()) {\/\/error} else {\/\/process}<\/li>\r\n \t<li>\u00b7\u00a0\u00a0\u00a0\u00a0\u00a0 If (!fobj) {\/\/error} else {\/\/process}<\/li>\r\n \t<li>\u00b7\u00a0\u00a0\u00a0\u00a0\u00a0 if (fobj) {\/\/process} else {\/\/error}<\/li>\r\n<\/ul>\r\n<strong>5.2.\u00a0\u00a0\u00a0 Closing file stream<\/strong>\r\n\r\n&nbsp;\r\n\r\nWhen a file stream object goes out of scope, it is destructed by an implicit call to the destructor and the file is closed. A file can be explicitly closedby invoking close() function. When a file is closed, the file stream becomes empty, until it is reconnected to another file.\r\n\r\n&nbsp;\r\n\r\nTo close the file, use close() member function as follows:\r\n\r\ninfile.close();\u00a0 \/\/ explicitly closing file associated with infile file stream\r\n\r\nIt is asking the operating system to disconnect the file and save its final state. In other words, it disconnects the file stream from the physical file.\r\n\r\n&nbsp;\r\n\r\n<strong>5.3.\u00a0\u00a0\u00a0 <\/strong>Text<strong> I\/O using files<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">In text I\/O, input\/output stream contains text data. In case of input, text data is read from input stream and transformed as per data type to be stored in memory; whereas in case of output, data from memory is transformed into text and stored in output stream.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">Unless binary is specified as file open mode, file is considered to be for text I\/O by default. With text I\/O, a file always contains data in human readable character form; character may be ASCII or Unicode. Once the file is opened, one can read and write values from\/to the streams using the stream input and output operators just like operations with cin and cout.\u00a0 Let us consider file named \u201cinp.txt\u201d with the content 24 567.89. An easy way to prepare an input file is to use a text editor that creates plain ASCII text files. To avoid some odd end-of-file behaviour, be sure that the last character in the file is a whitespace character such as a newline.To use this text file, first create input file stream object and connect this stream to physical file inp.txt as follows:<\/p>\r\n&nbsp;\r\n\r\ninpFile = ifstream.open(\u201cinp.txt\u201d);\r\n\r\n<\/div>\r\n<div>\r\n<p style=\"text-align: justify\">Above statement will create input file streamassociated with object inpFileand connect it to file named \u201cinp.txt\u201d for reading purpose. Note that the default file open mode is ios::in for ifstream. Once the file is opened successfully, it can be used for input operations. Since ifstream and ofstream inherit from istream and ostream, the definitions of overloaded operators &lt;&lt; or &gt;&gt; for ostream and istream automatically work for file streams also. Here, to read an integer and a double value from the input file intovariables say intVar and dblVar using &gt;&gt; extraction operator, one can write following statement:<\/p>\r\n&nbsp;\r\n\r\ninpFile&gt;&gt;intVar&gt;&gt;dblVar;\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Similarly to output to the text file named can be performed using &lt;&lt; insertion operator. Assuming file \u201cout.txt\u201d connected with output file stream associated with outFile object, one may use &lt;&lt; insertion operator as follows:<\/p>\r\n&nbsp;\r\n\r\noutFile&lt;&lt; \"The integer is \" &lt;&lt;intVar&lt;&lt;endl;\r\n\r\noutFile&lt;&lt; \"The double is \" &lt;&lt;dblVar&lt;&lt;endl;\r\n\r\nOnce reading from or writing to a file is finished, it is good programming practice to close the file.\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Since ifstreamis derived from istream, it inherits its member functions also to read data from input stream istream. Thus like extraction operator, one can also use get() or getline() functions to read characters or long sequence of characters such as lines. Various forms of functions get() and getline() are already explained in previous module while discussing I\/O using console stream cin. To use these functions with input files, the only change to be made is to use input file stream object instead of cin object. In the same way, as ofstream is derived from ostream, it inherits member functions also to write data into output stream ostream. Like insertion operator, one can also use put() to write a character into output file stream.With files, one needs to use output file stream object in place ofcout object. Because all these stream classes are inherited form ios_base, stream error status and format flags are also inherited in file stream classes. Thus one can use iostream error bits as well as perform formatted input\/output operations with file streams in a way similar to console I\/O streams cin and cout. Functions good(), bad(), eof(), fail() etc. can also be used with file streams to check error after any file operation.<\/p>\r\n&nbsp;\r\n\r\n<strong>5.4.\u00a0\u00a0\u00a0 Binary I\/O using files<\/strong>\r\n\r\n&nbsp;\r\n\r\nThe numbers are stored in computer\u2019s memory in binary format rather than as string of characters. Text I\/O operations require a transformation between numeric values stored in memory and the corresponding sequence of characters. Binary I\/O saves this transformation time and thus provides more efficiency. Another advantage is of saving space. In binary I\/O an int is stored in 4 bytes, whereas its text version might be \u201c123456789\u201d, requiring 9 bytes. Thus using files in binary has the advantages of speed as well as space over text files. The only disadvantage is human readability. Such files cannot be read using text editors.\r\n\r\n&nbsp;\r\n\r\nFor binary I\/O, file should be opened using the mode flag ios_base::binary. As an effect, it suppresses the formatting usually performed.\u00a0Binary inputand<span style=\"text-align: initial;font-size: 1em\"> output is done solely by using read() and <\/span>write<span style=\"text-align: initial;font-size: 1em\">() functions of <\/span>istream<span style=\"text-align: initial;font-size: 1em\"> and <\/span>ostream<span style=\"text-align: initial;font-size: 1em\"> class respectively.<\/span>\r\n\r\n<\/div>\r\n<div>\r\n\r\nReading\/writing from\/to binary file:\r\n\r\n&nbsp;\r\n\r\nSyntax:\r\n\r\n&nbsp;\r\n\r\nistream&amp;istream::read(char* str, int count)ostream&amp;ostream::write(const char* str, int count)\r\n\r\nFunction read() reads a block of binary data or reads a fixed number of bytes from the specified stream and store it in an C-type array str.\r\n\r\nFunction write() writes a block of binary data or writes fixed number of bytes from a specific memory location of strto the specified stream.\r\n\r\n&nbsp;\r\n\r\nNote:\r\n\r\n&nbsp;\r\n\r\nBoth functions take two arguments.\r\n\r\n\u00b7\u00a0\u00a0\u00a0\u00a0\u00a0 The first is the address of variable and the second is usually the length of that variable in bytes. Theaddress of variable must be type cast to type char*pointer.\r\n\r\n\u00b7\u00a0\u00a0\u00a0\u00a0\u00a0 The data written to a file using write( ) can only be read accurately using read().\r\n\r\n&nbsp;\r\n\r\n<strong>6.\u00a0\u00a0\u00a0\u00a0\u00a0 Random Access in Files<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Data from the file can be read sequentially as well as randomly from any position. Sequential data access may not be appropriate in some cases. For example, modify phone number of specific student, display balance amount of specified account number in a bank, search specified train and display its information etc. All this operations will be very slow when performed with sequential access. To modify phone number of 10th student requires first 9 student\u2019s data to be read first.<\/p>\r\n&nbsp;\r\n\r\nRandom access of data provides faster retrieval of records. Moving to 10th student record without reading first 9 will enable the updating faster.\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">File pointer Random access enables read\/write data from any position. For random access, it requires first to place a read\/write file pointer at the required position in the file and then perform read\/write. The file pointer indicates the position in the file at which the next input\/output is to occur. In C++, there are two types of file positions pointers: get and put. Get position pointer specifies location for next read and it is available for istream or input mode. Put position pointer specifies the location for next write operation and is available for ostream or output mode. Member functions to set and get the file pointer Moving the file pointer in a file may be required for various operations like modification of data, deletion of data, searching data etc. Both istream and ostream provide member functions for repositioning the file pointer and getting the current position of file pointer. Functions seekg() \u2013 (seek get) and seekp() \u2013 (seek put) are used to set file pointer for input and output respectively,Similarly<span style=\"text-align: initial;font-size: 1em\"> member functions <\/span>tellg<span style=\"text-align: initial;font-size: 1em\">() \u2013 (tell get) and <\/span>tellp<span style=\"text-align: initial;font-size: 1em\">() \u2013 (tell put) are used to get file pointer for input and output respectively.<\/span><span style=\"text-align: initial;font-size: 1em\">Here,seekg() and tellg() are the member functions of istream; whereas seekp() and tellp() are member functions of ostream class.<\/span><span style=\"text-align: initial;font-size: 1em\">Functions <\/span>tellg<span style=\"text-align: initial;font-size: 1em\">() and <\/span>tellp<span style=\"text-align: initial;font-size: 1em\">() do not take any parameters. They return the current position of <\/span>file<span style=\"text-align: initial;font-size: 1em\"> pointer. <\/span>Return<span style=\"text-align: initial;font-size: 1em\"> value is of type <\/span>streampos<span style=\"text-align: initial;font-size: 1em\"> which is usually a long integer. Their syntax is as follows:<\/span><\/p>\r\n\r\n<\/div>\r\n<div>\r\n\r\n&nbsp;\r\n\r\nstreamposistream::tellg()\r\n\r\nstreamposostream::tellp()\r\n\r\nTo know the file size (in bytes), tellg() or tellp() can be used as shown below.\r\n\r\nfstreammyfile (\"student.bin\", ios::binary | ios::ate);\r\n\r\ncout&lt;&lt; \"size is: \" &lt;&lt;myfile.tellg() &lt;&lt; \" bytes\\n\";\r\n\r\nFunctions seekg() and seekp() take two arguments: offset and seek direction. Their syntax is:\r\n\r\nstreamposistream::seekg(streamoffloc=0, ios::seekdirrel=ios::beg)\r\n\r\nstreamposostream::seekp(streamoffloc=0, ios::seekdirrel=ios::beg)\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Here, second argument is the relative position of enum type ios::seekdir. It can take values ios::beg, ios::cur andios::end. Default value isios::beg denoting beginning of file. Values ios::cur denotes current position and ios::end denotes end of file.First argument is an integer value that specifies the offset location as number of bytes from relative position specified in second argument. Offset is of the type streamoff, normally a long integer. It may be \u2013ve, 0 or +ve. Default value is zero. Negative value indicates backward move, whereas positive value indicates forward move.<\/p>\r\n&nbsp;\r\n\r\nExamples of using istream member function seekg() for moving file pointer:\r\n\r\n&nbsp;\r\n\r\n\u2022\u00a0\u00a0\u00a0\u00a0\u00a0 inpFile.seekg()\u2013Moves file pointer to beginning of the file. As arguments are not specified, it uses default values. Thus it has same effect as inpFile.seekg(0, ios::beg)\r\n\r\n\u2022\u00a0\u00a0\u00a0\u00a0\u00a0 inpFile.seekg(n, ios:beg) - Moves file pointer to n bytes forward from beginning. It can be written as inpFile.seekg(n) also.\r\n\r\n\u2022\u00a0\u00a0\u00a0\u00a0\u00a0 inpFile.seekg(n, ios:end) -Moves file pointer to n bytes back from end of the file.\r\n\r\n\u2022\u00a0\u00a0\u00a0\u00a0\u00a0 inpFile.seekg(0, ios:end) - Moves file pointer to end of the file.\r\n\r\n\u2022\u00a0\u00a0\u00a0\u00a0\u00a0 inpFile.seekg(n, ios:cur) - Moves file pointer to n bytes forward or back from current position depending on whether n is positive or negative.\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Member function seekp() of ostream class can be used in the similar way for moving file pointer in output stream.Random access for search or update data is easier with fixed length records. For varying length records, one may need some ways to store the address of records in the data file and use these addresses as offset while searching records.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">Let us consider simple example of using fixed-length record binary file and use random access. Consider a file having student\u2019s records. Assume class student with attributes name, gender, birthdate,<\/p>\r\n\r\n<\/div>\r\n<div>\r\n\r\nphone; method input() asking the user to enter student\u2019s details and method display() to show student details on screen.\u00a0 Following is the code used to first create binary file \u2018student.dat\u2019 with student\u2019s detail and then access a record at random.\r\n\r\n&nbsp;\r\n\r\nofstreamfout(\u201cstudent.dat\", ios::binary);\r\n\r\n&nbsp;\r\n\r\n\/\/\u00a0 store student\u2019s data in file student s;\r\n\r\nint n=30; \/\/ assume 30 students for (int i=0; i&lt;n; i++)\r\n\r\n{\r\n\r\ns.input(); \/\/ input from user\r\n\r\n&nbsp;\r\n\r\nfout.write (reinterpret_cast&lt;const char *&gt; (&amp;s), sizeof(student)); \/\/ store\r\n\r\n}\r\n\r\n&nbsp;\r\n\r\nfout.close();\r\n\r\n&nbsp;\r\n\r\n\/\/\u00a0 access 7th record\r\n\r\n&nbsp;\r\n\r\nifstream fin (\u201cstudent.dat\u201d, ios::binary);\r\n\r\n&nbsp;\r\n\r\nfin.seekg( 6 * sizeof( studnet) ); \/\/ pointer at 7th record from beginning of file fin.read ((char*)&amp;s, sizeof(s)); \/\/ may use reinterpret_cast for type conversion s.display();\r\n\r\n&nbsp;\r\n\r\nAbove code can be modified as per need. Readers may try to rewrite the code to update phone number of the specified student.\r\n\r\n&nbsp;\r\n\r\nSummary\r\n\r\n&nbsp;\r\n\r\n\u00b7\u00a0\u00a0\u00a0\u00a0\u00a0 Files are to be used when there is large input or when the same data is needed by multiple programs.\r\n\r\n\u00b7\u00a0\u00a0\u00a0\u00a0\u00a0 Files store persistent data in non-volatile secondary storage devices.\r\n\r\n\u00b7\u00a0\u00a0\u00a0\u00a0\u00a0 Files are managed by operating system.\r\n\r\n\u00b7\u00a0\u00a0\u00a0\u00a0\u00a0 C++ provides file streams ifstream, ofstream and fstream which are inherited from istrem, ostream and iostream respectively.\r\n\r\n&nbsp;\r\n\r\no All operations that can be performed with standard streams (cin and cout) can be performed with file streams also. Thus input can be perfromed using extraction operator and member functions get, getline, read; output can be performed using insertion operator and methods like put and write.\r\n\r\no Error status flag and formatting flags are also used the same way as with standard streams.\r\n\r\n\u00b7\u00a0\u00a0\u00a0\u00a0\u00a0 As an extenstion, file stream classes provides some other methods to open file, to check whether file is opened successfully or not (is_open()) andclose file.\r\n\r\n\u00b7\u00a0\u00a0\u00a0\u00a0\u00a0 File streams are required to be created by programmer. They are not available like standard streams.\r\n\r\n\u00b7\u00a0\u00a0\u00a0\u00a0\u00a0 File streams can be created using parameterized as well as non-parameterized constructors. With non-parameterized constructor, it simply creates a stream that is not connected to any file. With parameterized constructor, it creates a stream and assocites it with physical file.\r\n\r\n\u00b7\u00a0\u00a0\u00a0\u00a0\u00a0 Files can be opened with various modes like in, out, app, ate, trunc, binary, nocreate, noreplace. By defaut,\r\n\r\no\u00a0 files are considered for text i\/o operations\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">o <\/span>ifstream<span style=\"text-align: initial;font-size: 1em\"> is considered for input <\/span>opearions<span style=\"text-align: initial;font-size: 1em\"> o <\/span>ofstream<span style=\"text-align: initial;font-size: 1em\"> is considered for output operations\u00a0<\/span><span style=\"text-align: initial;font-size: 1em\">\u00a0<\/span>fstream<span style=\"text-align: initial;font-size: 1em\"> is considered for both input as well as output<\/span>\r\n\r\n<\/div>\r\n<ul>\r\n \t<li>Binary files are more efficient in terms of speed and memory usage. Read and write methods are to be used for binary read\/write operations.<\/li>\r\n \t<li>Files can be accessed randomly allowing faster retrieval of data. Methods seekg and seekp can be used to move file pointer at specific location before next read and write respectively. Methods tellg and tellp can be used to know the current file pointer position.<\/li>\r\n<\/ul>\r\n&nbsp;\r\n\r\n<strong>Additional References<\/strong>\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\n1) Stanley Lippmann, \u201cC++ Primer\u201d, Pearson Education.\r\n\r\n2) Bjarne Stroustrup, \u201cThe C++ Programming Language\u201d, Pearson Education.\r\n\r\n3) Scott Mayer, \u201cEffective C++\u201d, Addison Wesley.\r\n\r\n4) Bhushan Trivedi , Programming with ANSI C++, 2\/e , Oxford University Press.\r\n\r\n5) Yashavant P. Kanetkar \u201cLet Us C++\u201d , Bpb Publications.\r\n\r\n6) Abhiram G. Ranade, \u201cAn Introduction to Programming through C++\u201d , McGraw Hill\r\n\r\n7) Ellis and B. Stroustrup \u201cAnnotated C++ Reference Manual\u201d, http:\/\/www.stroustrup.com\/arm.html\r\n\r\n8) Herbert Schildt, \u201cComplete Reference C++\u201d, McGraw Hill Publications.\r\n\r\n9) Ashok Kamthane, \u201cObject Oriented Programming with ANSI and Turbo C++\u201d, Pearson Education\r\n\r\n10) E Balaguruswami, \u201cObject Oriented Programming With C++\u201d, Tata McGraw Hill\r\n\r\n11) \u201cC++ FAQs\u201d, Pearson Education.\r\n\r\n&nbsp;","rendered":"<div>\n<p><strong>Introduction<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">So far, we have used predefined standard I\/O streams cin and cout to read input from standard input device and write output to standard output device. Here data storage is transient. We will now extend this concept to disk files.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Files are used for persistent data storage. Most of the applications require storing data in files and use it later. Files usually resides on secondary storage devices like disk. In this module, we will see input\/output operations using file streams.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>1.\u00a0\u00a0\u00a0\u00a0\u00a0 File<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>File is used for persistent data storage. It resides on non- volatile secondary storage devices to retain the data for longer time. Data in files gets lost only when someone erases the data or when hardware storage media fails due to some reasons. Each file has End-Of-File (EOF) marker and it varies as per operating system. Actually all files are managed by operating system.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>2.\u00a0 Need of file<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Terminal I\/O using streams cin and cout stores the data in iostreams temporarily. Data in these streams are lost when the execution of an application gets terminated or when power fails. To have the data available even after program termination, it should be stored in files on non-volatile memory devices like disks. In most of the applications, the data needs to be retained for later use. Data may be created by one program and used by many programs. For example, name and registration number of students may be used by applications in accounts department, exam department. In railway reservation system, it requires to store the details about trains, schedule, fare, ticket booking etc. in persistent storage.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Sometimes when data size is large, one may like to create data in file and then use it. For example, giving input for a small matrix of size 10 x 10 requires 100 elements to be entered. A small mistake while entering data may require repeating entry of all elements. Here, it is better to create one text file and enter 100 elements in it. These data can be read by program to perform matrix operation. It will help to reduce data entry time and the chances of errors.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>3.\u00a0 File Streams<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Working with files requires the use of file-oriented classes: ifstream for input, ofstream for output and fstream for both input and output. Class ifstream (input file stream) is derived from istream, ofstream (output file stream) is derived from ostream and fstream (file stream) is derived from iostream. The ancestor classes (istream, ostream and iostream) are in turn derived from class ios. The file-oriented classes are also derived from the fstreambase class with multiple inheritances. This fstreambase class contains an object of class filebuf, providing a file-oriented buffer and its associated member functions. Class filebuf is derived from the more general streambufclass.<span style=\"text-align: initial;font-size: 1em\">Classesifstream, <\/span>ofstream and fstream<span style=\"text-align: initial;font-size: 1em\"> are declared in the <\/span>fstream<span style=\"text-align: initial;font-size: 1em\"> file. Thus, to perform file processing in C++, header files &lt;iostream&gt; and &lt;fstream&gt; must be included in C++ source file.<\/span>ofstreamobject<span style=\"text-align: initial;font-size: 1em\"> represents the output file stream and is used to create <\/span>filesand<span style=\"text-align: initial;font-size: 1em\">\/or write information to files.\u00a0<\/span>ifstream<span style=\"text-align: initial;font-size: 1em\"> object represents the input file stream and is used to <\/span>readinformation<span style=\"text-align: initial;font-size: 1em\"> from files.<\/span>fstream<span style=\"text-align: initial;font-size: 1em\"> object represents the file stream having <\/span>capabilitiesof<span style=\"text-align: initial;font-size: 1em\"> both <\/span>ofstream<span style=\"text-align: initial;font-size: 1em\"> and <\/span>ifstream<span style=\"text-align: initial;font-size: 1em\"> which means it can create files, <\/span>writeinformation<span style=\"text-align: initial;font-size: 1em\"> to files, and read information from files.<\/span><\/p>\n<\/div>\n<div>\n<p>&nbsp;<\/p>\n<p><strong>4.\u00a0\u00a0\u00a0\u00a0\u00a0 Differences between a predefined standard stream and a file stream<\/strong><\/p>\n<p><strong>\u00a0<\/strong><\/p>\n<p>\u00b7\u00a0\u00a0\u00a0\u00a0\u00a0 A file stream needs to be created and connected to a file before it can be used. The predefined streams are available to all programs and can be used right away.<\/p>\n<p>\u00b7\u00a0\u00a0\u00a0\u00a0\u00a0 Even though read\/write can be performed from any position with basic iostreams; this usually does not make any sense with the predefined streams, as they are connected to the sequential devices by default. One can reposition a file stream to arbitrary file positions and access data randomly also.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>5.\u00a0\u00a0\u00a0\u00a0\u00a0 File operations<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>When working with files, it requires file operations like connecting the physical file with file stream, opening the file, reading\/writing, checking errors and closing the file.<\/p>\n<p>\/\/\u00a0 basic file operations #include &lt;iostream&gt; #include &lt;fstream&gt; usingnamespacestd;<\/p>\n<p>int main ()<\/p>\n<p>&nbsp;<\/p>\n<p>{<\/p>\n<p>ofstreamoutfile;<\/p>\n<p>outfile.open (&#8220;example.txt&#8221;);<\/p>\n<p>outfile&lt;&lt; &#8220;This is my first attempt to use a file\\n&#8221;;<\/p>\n<p>outfile.close();<\/p>\n<p>return 0;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">As file stream classes are derived from i\/o stream classes, it is easy to read\/write from files. All the members of base classes are available to file classes. All of the member functions and operators that one can apply to istream or ostream or iostream object can also be applied to ifstream and ofstream and fstream objects. Thus reading and writing operations using file stream objects are very similar to reading\/writing using standard streams cin and cout.As an extension of the base classes, file streams have some additional member functions and internal information.Internally file objects use a special kind of stream buffer, called a file buffer, to control transferring characters to\/from the associated file.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>5.1.\u00a0\u00a0\u00a0 Opening a file<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Before performing input\/output operations on file, it requires a file to be opened. When opening a file, it requires supplying the physical file name and its location. The operating system (OS) searches its file system for the file of that name or create file with specified name.<\/p>\n<p>&nbsp;<\/p>\n<p>The OS will look for a file with specified name in thesame directory as of executable C++ files or project. If the file is placedin other directory, it should be specified with exact path.<\/p>\n<\/div>\n<div>\n<p>A file can be opened in two ways:<\/p>\n<p>&nbsp;<\/p>\n<p>1.\u00a0\u00a0\u00a0\u00a0 Using parameterized constructor &#8211; Open a file on construction of a file stream<\/p>\n<p>2.\u00a0\u00a0\u00a0\u00a0 Using open () function to connect a file with existing file stream<\/p>\n<p>&nbsp;<\/p>\n<p><strong>5.1.1. Open a file on <\/strong>construction<strong> of a file stream using parameterized constructor<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">While creating file stream object, specify a file name with file path as argument. It performs both the tasks of creating a file stream and connecting it to a specified file in a single statement.If an operation fails due to some reason, file stream is not ready for use.<\/p>\n<p>&nbsp;<\/p>\n<p>Example:<\/p>\n<p style=\"text-align: justify\">ifstreaminfile (\u201cinp.dat\u201d); \/\/create file stream infile, connect to file named \u2018inp.dat\u2019 if (!infile) \/\/ check error: unable to open file for input File will be closed automatically on destruction of the stream object.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>5.1.2. Using open () function to connect a file with existing file stream<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>Here, it requires two statements: first to create file stream and second to connect the file.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">1.\u00a0\u00a0\u00a0\u00a0\u00a0 Create anempty file stream using constructor without parameter. Here, a file stream is created that is not connected to any file. Any operation on the file stream will fail.<\/p>\n<p style=\"text-align: justify\">2.\u00a0\u00a0\u00a0\u00a0\u00a0 When needed, invoke open() function with file name with file path as parameter to connect it to the file stream.If the specified file cannot be opened due to some reason, failbit will be set; otherwise file stream is ready for use.<\/p>\n<p>&nbsp;<\/p>\n<p>Example:<\/p>\n<p>&nbsp;<\/p>\n<p>ifstreaminfile; \\\\ 1- file stream infile is created, but not connected to any file \u2026<\/p>\n<p>infile.open(\u201cinp.dat\u201d); \\\\ 2 &#8211; file \u2018inp.dat\u2019 is opened and connected to file stream infile if (!infile) \/\/ error: unable to open file for input<\/p>\n<p>&nbsp;<\/p>\n<p>Advantages of using this method of creating file first and connecting later are as follows:<\/p>\n<p>&nbsp;<\/p>\n<p>\u00b7\u00a0\u00a0\u00a0\u00a0\u00a0 Once a stream is created, it can be used to connect to any file later using open().<\/p>\n<p>\u00b7\u00a0\u00a0\u00a0\u00a0\u00a0 It enables the programmer to bind a file stream to more than one file during the lifetime of a stream.<\/p>\n<p>\u00b7\u00a0\u00a0\u00a0\u00a0\u00a0 Closing the file does not destroy the stream. It simply gets disconnected from the physical file. Stream is still available for another physical file to get connected.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>5.1.3. File open mode<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Files are opened using default mode when modeis not specified. It defaults to text file with input mode for ifstream; output mode for ofstream and input as well as output modes for fstream. Sometimes, it may be required to change this default. For example, one may want to have binary I\/O; or want to append data in existing file. For this, C++ provides various modes that can be used while opening the file. Open mode is defined in ios_base class.The ios_base::openmode is of a bitmask type like the format flags and the stream state. The bits and the meaning of open mode are as follows:<\/p>\n<p>&nbsp;<\/p>\n<table class=\"aligncenter\">\n<tbody>\n<tr>\n<td style=\"width: 153.063px\">Open mode bit<\/td>\n<td style=\"width: 507.063px\">Meaning<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 153.063px\">ios_base::in<\/td>\n<td style=\"width: 507.063px\">Open file for reading. File must exist. Default for<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 153.063px\"><\/td>\n<td style=\"width: 507.063px\">ifstream.<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 153.063px\">ios_base::out<\/td>\n<td style=\"width: 507.063px\">Open file for writing. If file exists, existing file is<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 153.063px\"><\/td>\n<td style=\"width: 507.063px\">deleted and a new empty file is created with same<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 153.063px\"><\/td>\n<td style=\"width: 507.063px\">name. If file does not exist, it will create a new<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<div>\n<table class=\"aligncenter\">\n<tbody>\n<tr>\n<td><\/td>\n<td>file. Default for ofstream.<\/td>\n<\/tr>\n<tr>\n<td>ios_base::ate<\/td>\n<td>Start position for reading or writing is at end of<\/td>\n<\/tr>\n<tr>\n<td><\/td>\n<td>file (AT End).<\/td>\n<\/tr>\n<tr>\n<td>ios_base::app<\/td>\n<td>Start\u00a0 position for\u00a0 writing is\u00a0 at\u00a0 end of\u00a0 file<\/td>\n<\/tr>\n<tr>\n<td><\/td>\n<td>(APPend)<\/td>\n<\/tr>\n<tr>\n<td>ios_base::trunk<\/td>\n<td>Truncate\u00a0 file\u00a0 to\u00a0 zero\u00a0 length\u00a0 if\u00a0 it\u00a0 exists<\/td>\n<\/tr>\n<tr>\n<td><\/td>\n<td>(TRUNCate)<\/td>\n<\/tr>\n<tr>\n<td>ios_base::binary<\/td>\n<td>Open file in Binary mode for binary I\/O<\/td>\n<\/tr>\n<tr>\n<td>ios_base::nocreate<\/td>\n<td>Do not allow to create a new file. When opening,<\/td>\n<\/tr>\n<tr>\n<td><\/td>\n<td>file must exist. If the file to be opened does not<\/td>\n<\/tr>\n<tr>\n<td><\/td>\n<td>exist, an error will occur.<\/td>\n<\/tr>\n<tr>\n<td>ios_base::noreplace<\/td>\n<td>Do not allow to replace the content of file. Error<\/td>\n<\/tr>\n<tr>\n<td><\/td>\n<td>will occur when opening file for output if file<\/td>\n<\/tr>\n<tr>\n<td><\/td>\n<td>already exists, unless ate or app is set.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p>Default file open mode is as follows:<\/p>\n<table class=\"aligncenter\">\n<tbody>\n<tr>\n<td style=\"width: 251.063px\">File Stream<\/td>\n<td style=\"width: 409.063px\">Default Open Mode<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 251.063px\">ifstream<\/td>\n<td style=\"width: 409.063px\">in<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 251.063px\">ofstream<\/td>\n<td style=\"width: 409.063px\">out|trunc<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 251.063px\">fstream<\/td>\n<td style=\"width: 409.063px\">in|out|trunc<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">File open mode flag bits can be combined for effects of multiple flags using OR(|). For example, ios::out|ios::app\u00a0 Open file for writing by appending new content. It creates file if it does not exist. If file already exists, file pointer is positioned at the end.<\/p>\n<p>ios::in|ios::out<\/p>\n<p>Open file for reading as well as writing. File must exist.<\/p>\n<p>ios::in|ios::out | ios::trunc<\/p>\n<p>&nbsp;<\/p>\n<p>Open file for reading as well as writing. It empties fileif it exists, creates otherwise.<\/p>\n<p>&nbsp;<\/p>\n<p>While opening the file, file name and open mode can be passed as two parameters in constructor as well as open() function.<\/p>\n<p>&nbsp;<\/p>\n<p>\u00b7\u00a0\u00a0\u00a0\u00a0\u00a0 Constructor: &lt;file stream&gt; (const char *filename, ios_base::openmode mode);<\/p>\n<p>\u00b7\u00a0\u00a0\u00a0\u00a0\u00a0 Function open(): void open(const char *filename, ios_base::openmode mode);<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">In the given examples, constructor and open() are used with file name only as an argument. In absence of second argument, default value of file open mode is considered depending upon the type of file stream object.One can explicitly specify the second argument for open mode in the file stream constructor while creating file stream object or in open() call.<\/p>\n<p>&nbsp;<\/p>\n<p>For example,<\/p>\n<p>&nbsp;<\/p>\n<p>ofstreamfout(\u201cout.txt\u201d,ios_base::out|ios_base::app);<\/p>\n<p>ofstreamoutfile;<\/p>\n<p>outfile.open(\u201cstudents.txt\u201d, ios::out|ios::app);<\/p>\n<p>Note that class ios is derived from ios_base, so all the members of ios_base class are inherited in ios class.<\/p>\n<\/div>\n<div>\n<p>5.1.4. Various ways to test whether a file is opened successfully or not<\/p>\n<p>&nbsp;<\/p>\n<p>To test whether the file is successfully opened or not, one way is to use is_open() method. One may even check error state bits and use methods good(), bad(), fail(), rdstate() etc. as described with standard streams.<\/p>\n<ul>\n<li>\u00b7\u00a0\u00a0\u00a0\u00a0\u00a0 Function is_open() returns true if open is successful<\/li>\n<li>\u00b7\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Functions good()returns true if open is successful, bad() and fail() returns true when unsuccessful<\/li>\n<li>\u00b7\u00a0\u00a0\u00a0\u00a0\u00a0 Operator !returns true when open is unsuccessful<\/li>\n<li>\u00b7\u00a0\u00a0\u00a0\u00a0\u00a0 Stream pointer is null when open is unsuccessful<\/li>\n<\/ul>\n<p>Examples:<\/p>\n<p>&nbsp;<\/p>\n<p>fstreamfobj; fobj.open(\u201cdata.txt\u201d, ios::in | ios::out);<\/p>\n<ul>\n<li>\u00b7\u00a0\u00a0\u00a0\u00a0\u00a0 if (fobj.is_open()) {\/\/process} else {\/\/error}<\/li>\n<li>\u00b7\u00a0\u00a0\u00a0\u00a0\u00a0 if (fobj.good()) {\/\/process} else {\/\/error}<\/li>\n<li>\u00b7\u00a0\u00a0\u00a0\u00a0\u00a0 If (fobj.fail()) {\/\/error} else {\/\/process}<\/li>\n<li>\u00b7\u00a0\u00a0\u00a0\u00a0\u00a0 If (fobj.bad()) {\/\/error} else {\/\/process}<\/li>\n<li>\u00b7\u00a0\u00a0\u00a0\u00a0\u00a0 If (!fobj) {\/\/error} else {\/\/process}<\/li>\n<li>\u00b7\u00a0\u00a0\u00a0\u00a0\u00a0 if (fobj) {\/\/process} else {\/\/error}<\/li>\n<\/ul>\n<p><strong>5.2.\u00a0\u00a0\u00a0 Closing file stream<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>When a file stream object goes out of scope, it is destructed by an implicit call to the destructor and the file is closed. A file can be explicitly closedby invoking close() function. When a file is closed, the file stream becomes empty, until it is reconnected to another file.<\/p>\n<p>&nbsp;<\/p>\n<p>To close the file, use close() member function as follows:<\/p>\n<p>infile.close();\u00a0 \/\/ explicitly closing file associated with infile file stream<\/p>\n<p>It is asking the operating system to disconnect the file and save its final state. In other words, it disconnects the file stream from the physical file.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>5.3.\u00a0\u00a0\u00a0 <\/strong>Text<strong> I\/O using files<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">In text I\/O, input\/output stream contains text data. In case of input, text data is read from input stream and transformed as per data type to be stored in memory; whereas in case of output, data from memory is transformed into text and stored in output stream.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Unless binary is specified as file open mode, file is considered to be for text I\/O by default. With text I\/O, a file always contains data in human readable character form; character may be ASCII or Unicode. Once the file is opened, one can read and write values from\/to the streams using the stream input and output operators just like operations with cin and cout.\u00a0 Let us consider file named \u201cinp.txt\u201d with the content 24 567.89. An easy way to prepare an input file is to use a text editor that creates plain ASCII text files. To avoid some odd end-of-file behaviour, be sure that the last character in the file is a whitespace character such as a newline.To use this text file, first create input file stream object and connect this stream to physical file inp.txt as follows:<\/p>\n<p>&nbsp;<\/p>\n<p>inpFile = ifstream.open(\u201cinp.txt\u201d);<\/p>\n<\/div>\n<div>\n<p style=\"text-align: justify\">Above statement will create input file streamassociated with object inpFileand connect it to file named \u201cinp.txt\u201d for reading purpose. Note that the default file open mode is ios::in for ifstream. Once the file is opened successfully, it can be used for input operations. Since ifstream and ofstream inherit from istream and ostream, the definitions of overloaded operators &lt;&lt; or &gt;&gt; for ostream and istream automatically work for file streams also. Here, to read an integer and a double value from the input file intovariables say intVar and dblVar using &gt;&gt; extraction operator, one can write following statement:<\/p>\n<p>&nbsp;<\/p>\n<p>inpFile&gt;&gt;intVar&gt;&gt;dblVar;<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Similarly to output to the text file named can be performed using &lt;&lt; insertion operator. Assuming file \u201cout.txt\u201d connected with output file stream associated with outFile object, one may use &lt;&lt; insertion operator as follows:<\/p>\n<p>&nbsp;<\/p>\n<p>outFile&lt;&lt; &#8220;The integer is &#8221; &lt;&lt;intVar&lt;&lt;endl;<\/p>\n<p>outFile&lt;&lt; &#8220;The double is &#8221; &lt;&lt;dblVar&lt;&lt;endl;<\/p>\n<p>Once reading from or writing to a file is finished, it is good programming practice to close the file.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Since ifstreamis derived from istream, it inherits its member functions also to read data from input stream istream. Thus like extraction operator, one can also use get() or getline() functions to read characters or long sequence of characters such as lines. Various forms of functions get() and getline() are already explained in previous module while discussing I\/O using console stream cin. To use these functions with input files, the only change to be made is to use input file stream object instead of cin object. In the same way, as ofstream is derived from ostream, it inherits member functions also to write data into output stream ostream. Like insertion operator, one can also use put() to write a character into output file stream.With files, one needs to use output file stream object in place ofcout object. Because all these stream classes are inherited form ios_base, stream error status and format flags are also inherited in file stream classes. Thus one can use iostream error bits as well as perform formatted input\/output operations with file streams in a way similar to console I\/O streams cin and cout. Functions good(), bad(), eof(), fail() etc. can also be used with file streams to check error after any file operation.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>5.4.\u00a0\u00a0\u00a0 Binary I\/O using files<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>The numbers are stored in computer\u2019s memory in binary format rather than as string of characters. Text I\/O operations require a transformation between numeric values stored in memory and the corresponding sequence of characters. Binary I\/O saves this transformation time and thus provides more efficiency. Another advantage is of saving space. In binary I\/O an int is stored in 4 bytes, whereas its text version might be \u201c123456789\u201d, requiring 9 bytes. Thus using files in binary has the advantages of speed as well as space over text files. The only disadvantage is human readability. Such files cannot be read using text editors.<\/p>\n<p>&nbsp;<\/p>\n<p>For binary I\/O, file should be opened using the mode flag ios_base::binary. As an effect, it suppresses the formatting usually performed.\u00a0Binary inputand<span style=\"text-align: initial;font-size: 1em\"> output is done solely by using read() and <\/span>write<span style=\"text-align: initial;font-size: 1em\">() functions of <\/span>istream<span style=\"text-align: initial;font-size: 1em\"> and <\/span>ostream<span style=\"text-align: initial;font-size: 1em\"> class respectively.<\/span><\/p>\n<\/div>\n<div>\n<p>Reading\/writing from\/to binary file:<\/p>\n<p>&nbsp;<\/p>\n<p>Syntax:<\/p>\n<p>&nbsp;<\/p>\n<p>istream&amp;istream::read(char* str, int count)ostream&amp;ostream::write(const char* str, int count)<\/p>\n<p>Function read() reads a block of binary data or reads a fixed number of bytes from the specified stream and store it in an C-type array str.<\/p>\n<p>Function write() writes a block of binary data or writes fixed number of bytes from a specific memory location of strto the specified stream.<\/p>\n<p>&nbsp;<\/p>\n<p>Note:<\/p>\n<p>&nbsp;<\/p>\n<p>Both functions take two arguments.<\/p>\n<p>\u00b7\u00a0\u00a0\u00a0\u00a0\u00a0 The first is the address of variable and the second is usually the length of that variable in bytes. Theaddress of variable must be type cast to type char*pointer.<\/p>\n<p>\u00b7\u00a0\u00a0\u00a0\u00a0\u00a0 The data written to a file using write( ) can only be read accurately using read().<\/p>\n<p>&nbsp;<\/p>\n<p><strong>6.\u00a0\u00a0\u00a0\u00a0\u00a0 Random Access in Files<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Data from the file can be read sequentially as well as randomly from any position. Sequential data access may not be appropriate in some cases. For example, modify phone number of specific student, display balance amount of specified account number in a bank, search specified train and display its information etc. All this operations will be very slow when performed with sequential access. To modify phone number of 10th student requires first 9 student\u2019s data to be read first.<\/p>\n<p>&nbsp;<\/p>\n<p>Random access of data provides faster retrieval of records. Moving to 10th student record without reading first 9 will enable the updating faster.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">File pointer Random access enables read\/write data from any position. For random access, it requires first to place a read\/write file pointer at the required position in the file and then perform read\/write. The file pointer indicates the position in the file at which the next input\/output is to occur. In C++, there are two types of file positions pointers: get and put. Get position pointer specifies location for next read and it is available for istream or input mode. Put position pointer specifies the location for next write operation and is available for ostream or output mode. Member functions to set and get the file pointer Moving the file pointer in a file may be required for various operations like modification of data, deletion of data, searching data etc. Both istream and ostream provide member functions for repositioning the file pointer and getting the current position of file pointer. Functions seekg() \u2013 (seek get) and seekp() \u2013 (seek put) are used to set file pointer for input and output respectively,Similarly<span style=\"text-align: initial;font-size: 1em\"> member functions <\/span>tellg<span style=\"text-align: initial;font-size: 1em\">() \u2013 (tell get) and <\/span>tellp<span style=\"text-align: initial;font-size: 1em\">() \u2013 (tell put) are used to get file pointer for input and output respectively.<\/span><span style=\"text-align: initial;font-size: 1em\">Here,seekg() and tellg() are the member functions of istream; whereas seekp() and tellp() are member functions of ostream class.<\/span><span style=\"text-align: initial;font-size: 1em\">Functions <\/span>tellg<span style=\"text-align: initial;font-size: 1em\">() and <\/span>tellp<span style=\"text-align: initial;font-size: 1em\">() do not take any parameters. They return the current position of <\/span>file<span style=\"text-align: initial;font-size: 1em\"> pointer. <\/span>Return<span style=\"text-align: initial;font-size: 1em\"> value is of type <\/span>streampos<span style=\"text-align: initial;font-size: 1em\"> which is usually a long integer. Their syntax is as follows:<\/span><\/p>\n<\/div>\n<div>\n<p>&nbsp;<\/p>\n<p>streamposistream::tellg()<\/p>\n<p>streamposostream::tellp()<\/p>\n<p>To know the file size (in bytes), tellg() or tellp() can be used as shown below.<\/p>\n<p>fstreammyfile (&#8220;student.bin&#8221;, ios::binary | ios::ate);<\/p>\n<p>cout&lt;&lt; &#8220;size is: &#8221; &lt;&lt;myfile.tellg() &lt;&lt; &#8221; bytes\\n&#8221;;<\/p>\n<p>Functions seekg() and seekp() take two arguments: offset and seek direction. Their syntax is:<\/p>\n<p>streamposistream::seekg(streamoffloc=0, ios::seekdirrel=ios::beg)<\/p>\n<p>streamposostream::seekp(streamoffloc=0, ios::seekdirrel=ios::beg)<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Here, second argument is the relative position of enum type ios::seekdir. It can take values ios::beg, ios::cur andios::end. Default value isios::beg denoting beginning of file. Values ios::cur denotes current position and ios::end denotes end of file.First argument is an integer value that specifies the offset location as number of bytes from relative position specified in second argument. Offset is of the type streamoff, normally a long integer. It may be \u2013ve, 0 or +ve. Default value is zero. Negative value indicates backward move, whereas positive value indicates forward move.<\/p>\n<p>&nbsp;<\/p>\n<p>Examples of using istream member function seekg() for moving file pointer:<\/p>\n<p>&nbsp;<\/p>\n<p>\u2022\u00a0\u00a0\u00a0\u00a0\u00a0 inpFile.seekg()\u2013Moves file pointer to beginning of the file. As arguments are not specified, it uses default values. Thus it has same effect as inpFile.seekg(0, ios::beg)<\/p>\n<p>\u2022\u00a0\u00a0\u00a0\u00a0\u00a0 inpFile.seekg(n, ios:beg) &#8211; Moves file pointer to n bytes forward from beginning. It can be written as inpFile.seekg(n) also.<\/p>\n<p>\u2022\u00a0\u00a0\u00a0\u00a0\u00a0 inpFile.seekg(n, ios:end) -Moves file pointer to n bytes back from end of the file.<\/p>\n<p>\u2022\u00a0\u00a0\u00a0\u00a0\u00a0 inpFile.seekg(0, ios:end) &#8211; Moves file pointer to end of the file.<\/p>\n<p>\u2022\u00a0\u00a0\u00a0\u00a0\u00a0 inpFile.seekg(n, ios:cur) &#8211; Moves file pointer to n bytes forward or back from current position depending on whether n is positive or negative.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Member function seekp() of ostream class can be used in the similar way for moving file pointer in output stream.Random access for search or update data is easier with fixed length records. For varying length records, one may need some ways to store the address of records in the data file and use these addresses as offset while searching records.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Let us consider simple example of using fixed-length record binary file and use random access. Consider a file having student\u2019s records. Assume class student with attributes name, gender, birthdate,<\/p>\n<\/div>\n<div>\n<p>phone; method input() asking the user to enter student\u2019s details and method display() to show student details on screen.\u00a0 Following is the code used to first create binary file \u2018student.dat\u2019 with student\u2019s detail and then access a record at random.<\/p>\n<p>&nbsp;<\/p>\n<p>ofstreamfout(\u201cstudent.dat&#8221;, ios::binary);<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/\u00a0 store student\u2019s data in file student s;<\/p>\n<p>int n=30; \/\/ assume 30 students for (int i=0; i&lt;n; i++)<\/p>\n<p>{<\/p>\n<p>s.input(); \/\/ input from user<\/p>\n<p>&nbsp;<\/p>\n<p>fout.write (reinterpret_cast&lt;const char *&gt; (&amp;s), sizeof(student)); \/\/ store<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>fout.close();<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/\u00a0 access 7th record<\/p>\n<p>&nbsp;<\/p>\n<p>ifstream fin (\u201cstudent.dat\u201d, ios::binary);<\/p>\n<p>&nbsp;<\/p>\n<p>fin.seekg( 6 * sizeof( studnet) ); \/\/ pointer at 7th record from beginning of file fin.read ((char*)&amp;s, sizeof(s)); \/\/ may use reinterpret_cast for type conversion s.display();<\/p>\n<p>&nbsp;<\/p>\n<p>Above code can be modified as per need. Readers may try to rewrite the code to update phone number of the specified student.<\/p>\n<p>&nbsp;<\/p>\n<p>Summary<\/p>\n<p>&nbsp;<\/p>\n<p>\u00b7\u00a0\u00a0\u00a0\u00a0\u00a0 Files are to be used when there is large input or when the same data is needed by multiple programs.<\/p>\n<p>\u00b7\u00a0\u00a0\u00a0\u00a0\u00a0 Files store persistent data in non-volatile secondary storage devices.<\/p>\n<p>\u00b7\u00a0\u00a0\u00a0\u00a0\u00a0 Files are managed by operating system.<\/p>\n<p>\u00b7\u00a0\u00a0\u00a0\u00a0\u00a0 C++ provides file streams ifstream, ofstream and fstream which are inherited from istrem, ostream and iostream respectively.<\/p>\n<p>&nbsp;<\/p>\n<p>o All operations that can be performed with standard streams (cin and cout) can be performed with file streams also. Thus input can be perfromed using extraction operator and member functions get, getline, read; output can be performed using insertion operator and methods like put and write.<\/p>\n<p>o Error status flag and formatting flags are also used the same way as with standard streams.<\/p>\n<p>\u00b7\u00a0\u00a0\u00a0\u00a0\u00a0 As an extenstion, file stream classes provides some other methods to open file, to check whether file is opened successfully or not (is_open()) andclose file.<\/p>\n<p>\u00b7\u00a0\u00a0\u00a0\u00a0\u00a0 File streams are required to be created by programmer. They are not available like standard streams.<\/p>\n<p>\u00b7\u00a0\u00a0\u00a0\u00a0\u00a0 File streams can be created using parameterized as well as non-parameterized constructors. With non-parameterized constructor, it simply creates a stream that is not connected to any file. With parameterized constructor, it creates a stream and assocites it with physical file.<\/p>\n<p>\u00b7\u00a0\u00a0\u00a0\u00a0\u00a0 Files can be opened with various modes like in, out, app, ate, trunc, binary, nocreate, noreplace. By defaut,<\/p>\n<p>o\u00a0 files are considered for text i\/o operations<\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">o <\/span>ifstream<span style=\"text-align: initial;font-size: 1em\"> is considered for input <\/span>opearions<span style=\"text-align: initial;font-size: 1em\"> o <\/span>ofstream<span style=\"text-align: initial;font-size: 1em\"> is considered for output operations\u00a0<\/span><span style=\"text-align: initial;font-size: 1em\">\u00a0<\/span>fstream<span style=\"text-align: initial;font-size: 1em\"> is considered for both input as well as output<\/span><\/p>\n<\/div>\n<ul>\n<li>Binary files are more efficient in terms of speed and memory usage. Read and write methods are to be used for binary read\/write operations.<\/li>\n<li>Files can be accessed randomly allowing faster retrieval of data. Methods seekg and seekp can be used to move file pointer at specific location before next read and write respectively. Methods tellg and tellp can be used to know the current file pointer position.<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p><strong>Additional References<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>1) Stanley Lippmann, \u201cC++ Primer\u201d, Pearson Education.<\/p>\n<p>2) Bjarne Stroustrup, \u201cThe C++ Programming Language\u201d, Pearson Education.<\/p>\n<p>3) Scott Mayer, \u201cEffective C++\u201d, Addison Wesley.<\/p>\n<p>4) Bhushan Trivedi , Programming with ANSI C++, 2\/e , Oxford University Press.<\/p>\n<p>5) Yashavant P. Kanetkar \u201cLet Us C++\u201d , Bpb Publications.<\/p>\n<p>6) Abhiram G. Ranade, \u201cAn Introduction to Programming through C++\u201d , McGraw Hill<\/p>\n<p>7) Ellis and B. Stroustrup \u201cAnnotated C++ Reference Manual\u201d, http:\/\/www.stroustrup.com\/arm.html<\/p>\n<p>8) Herbert Schildt, \u201cComplete Reference C++\u201d, McGraw Hill Publications.<\/p>\n<p>9) Ashok Kamthane, \u201cObject Oriented Programming with ANSI and Turbo C++\u201d, Pearson Education<\/p>\n<p>10) E Balaguruswami, \u201cObject Oriented Programming With C++\u201d, Tata McGraw Hill<\/p>\n<p>11) \u201cC++ FAQs\u201d, Pearson Education.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"author":4,"menu_order":28,"template":"","meta":{"pb_show_title":"on","pb_short_title":"","pb_subtitle":"","pb_authors":["dr-jyoti-pareek"],"pb_section_license":""},"chapter-type":[],"contributor":[58],"license":[],"class_list":["post-209","chapter","type-chapter","status-publish","hentry","contributor-dr-jyoti-pareek"],"part":3,"_links":{"self":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-json\/pressbooks\/v2\/chapters\/209","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-json\/pressbooks\/v2\/chapters"}],"about":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-json\/wp\/v2\/types\/chapter"}],"author":[{"embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-json\/wp\/v2\/users\/4"}],"version-history":[{"count":6,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-json\/pressbooks\/v2\/chapters\/209\/revisions"}],"predecessor-version":[{"id":345,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-json\/pressbooks\/v2\/chapters\/209\/revisions\/345"}],"part":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-json\/pressbooks\/v2\/parts\/3"}],"metadata":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-json\/pressbooks\/v2\/chapters\/209\/metadata\/"}],"wp:attachment":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-json\/wp\/v2\/media?parent=209"}],"wp:term":[{"taxonomy":"chapter-type","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-json\/pressbooks\/v2\/chapter-type?post=209"},{"taxonomy":"contributor","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-json\/wp\/v2\/contributor?post=209"},{"taxonomy":"license","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-json\/wp\/v2\/license?post=209"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}