setiosflags(long) Sets format state as specified by argument setw(int) Sets field width. resetiosflags(long) Clears format state as specified by argument The stream classes have a variety of member functions to give them their required functionalities. Thus, there is a function to open the stream, one for reading/writing, one for closing the stream and the like. The stream class member functions are listed below Table 11.2The stream class member functions Continued… 250 CU IDOL SELF LEARNING MATERIAL (SLM)
Table 11.2The stream class member function 10.4 FILE OPERATIONS A file may be opened for a number of file operations. The corresponding stream must be set with the intended operation. The different file stream modes are indicated by File Access Flags as listed below: 11.3File Access Flags This program displays use of different stream manipulators. 251 CU IDOL SELF LEARNING MATERIAL (SLM)
The output of the program: Decimal…………….100 Hexadecimal…………6 Octal………………..144 The put() and get() Functions The classes istream and ostream define two member functions get() and put() respectively to handle the single character input/output operations. There are two types of get() functions. We can use both get(char*) and get(void) prototypes to fetch a character including the blank space, tab and the newline character. The get(char*) version assigns the input character to its argument and the get(void) version returns the input character. . . Since these functions are 252 CU IDOL SELF LEARNING MATERIAL (SLM)
members of the input/output stream classes, we must invoke them using an appropriate object. For instance, look at the code snippet given below: char c; cin.get(c); //get a character from keyboard and assign it to c while (c!= ‘\\n’) { cout << c; //display the character on screen cin.get (c); //get another character } This code reads and displays a line of text (terminated by a newline character). Remember, the operator>>can also be used to read a character but it will skip the white spaces and newline character. The above while loop will not work properly if the statement. cin >> c; is used in place of cin.get(c); Try using both of them and compare the results. The get(void) version is used as follows: char c; c = cin.get(); //cin.get(c) replaced The value returned by the function get() is assigned to the variable c. The function put(), a member of ostream class, can be used to output a line of text, character by character. For example, cout << put(‘x’); displays the character x and cout << put(ch); displays the value of variable ch. The variable ch must contain a character value. We can also use a number as an argument to 253 CU IDOL SELF LEARNING MATERIAL (SLM)
the function put (). For example, cout << put(68); displays the character D. This statement will convert the int value 90 to a char value and display the character whose ASCII value is 68. The following segment of a program reads a line of text from the keyboard and displays it on the screen. char c; cin.get (c); //read a character while(c!= ‘\\n’) { cout << put(c); //display the character on screen cin.get (c ); The getline () and write () Functions We can read and display a line of text more efficiently using the line-oriented input/output functions getline() and write(). The getline() function reads a whole line of text that ends with a newline character. This function can be invoked by using the object cin as follows: cin.getline(line, size); This function call invokes the function which reads character input into the variable line. The reading is terminated as soon as either the newline character ‘\\n’ is encountered or size number of characters are read (whichever occurs first). The newline character is read but not saved. Instead, it is replaced by the null character. For example; consider the following code: char name[20]; cin.getline(name, 20); Assume that we have given the following input through the keyboard: Neeraj good This input will be read correctly and assigned to the character array name. Let us suppose the input is as follows 254 CU IDOL SELF LEARNING MATERIAL (SLM)
11.5 UNFORMATTED I/O OPERATIONS Unformatted Input/output is the most basic form of input/output. Unformatted input/output transfers the internal binary representation of the data directly between memory and the file. Formatted output converts the internal binary representation of the data to ASCII characters which are written to the output file. Formatted input reads characters from the input file andconverts them to internal form. Formatted I/O can be either “Free” format or “Explicit” format, as described below. Advantages and Disadvantages of Unformatted I/O Unformatted input/output is the simplest and most efficient form of input/output. It is usually the most compact way to store data. Unformatted input/output is the least portable form of input/output. Unformatted data files can only be moved easily to and from computers that share the same internal data representation. It should be noted that XDR (eXternal Data Representation) files, described in Portable Unformatted Input/Output, can be used to produce portable binary data. Advantages and Disadvantages of Formatted I/O Formatted input/output is very portable. It is a simple process to move formatted data files to various computers, even computers running different operating systems, as long as they all use the ASCII character set. (ASCII is the American Standard Code for Information Interchange. It is the character set used by almost all current computers, with the notable exception of large IBM mainframes.) Formatted files are human readable and can be typed to the terminal screen or edited with a text editor. However, formatted input/output is more computationally expensive than unformatted input/ output because of the need to convert between internal binary data and ASCII text. Formatted data requires more space than unformatted to represent the same information. Inaccuracies can result when converting data between text and the internal representation. 11.6 READING/WRITING OF FILES 255 CU IDOL SELF LEARNING MATERIAL (SLM)
To read from a file, first we have to open the specified file in reading mode. If the file doesn’t exist, then its respective method returns NULL. The following program shows how to read the contents of a file in C++ − #include<iostream> #include<fstream> #include<string> usingnamespace std; int main (){ string readfile; ifstream myfile (\"Tempfile.txt\",ios::in); if(myfile.is_open()){ while( getline (myfile,readfile)){ cout << readfile <<'\\n'; } myfile.close(); }else cout <<\"file doesn't exist\"; return0; } It will produce the following output − Writing contents to file 256 CU IDOL SELF LEARNING MATERIAL (SLM)
Note − In this program, we opened a text file in read mode using “ios::in” and then print its contents on the screen. We have used while loop to read the file contents line by line by using “getline” method. The following program shows how to perform the same operation using Erlang. Here, we will use the read_file(filename) method to read all the contents from the specified file. -module(helloworld). -export([start/0]). start()-> rdfile = file:read_file(\"Tempfile.txt\"), io:fwrite(\"~p~n\",[rdfile]). It will produce the following output − ok, Writing contents to file Writing into a File To write contents into a file, we will first need to open the required file. If the specified file does not exist, then a new file will be created. Let’s see how to write contents into a file using C++. Example #include<iostream> #include<fstream> usingnamespace std; int main (){ ofstream myfile; 257 CU IDOL SELF LEARNING MATERIAL (SLM)
myfile.open (\"Tempfile.txt\", ios::out); myfile <<\"Writing Contents to file.\\n\"; cout <<\"Data inserted into file\"; myfile.close(); return0; } Note − fstream is the stream class used to control file read/write operations. ofstream is the stream class used to write contents into file. Let’s see how to write contents into a file using Erlang, which is a functional programming language. -module(helloworld). -export([start/0]). start()-> {ok,File1}= file:open(\"Tempfile.txt\",[write]), file:write(File1,\"Writting contents to file\"), io:fwrite(\"Data inserted into file\\n\"). Note − To open a file we have to use, open(filename,mode). Syntax to write contents to file: write(filemode,file_content). Output − When we run this code “Writing contents to file” will be written into the 258 CU IDOL SELF LEARNING MATERIAL (SLM)
file Tempfile.txt. If the file has any existing content, then it will be overwritten. 11.7SUMMARY C++ accomplishes input/output operations using concept of stream. A stream is a series of bytes whose value depends on the variable in which it is stored. This way, C++ is able to treat all the input and output operations in a uniform manner. Thus, whether it is reading from a file or from the keyboard, for a C++ program it is simply a stream. A stream is a source of sequence of bytes. A stream abstracts for input/output devices. It can be tied up with any I/O device and I/O can be performed in a uniform way. The C++ iostream library is an object-oriented implementation of this abstraction. It has a source (producer) of flow of bytes and a sink (consumer) of the bytes. The required classes for the stream I/O are defined in different library header files. Streams can also be tied up with data files. Unformatted input/output is the simplest and most efficient form of input/output. It is usually the most compact way to store data. Unformatted input/output is the least portable form of input/output. Files are used to store data in a storage device permanently. File handling provides a mechanism to store the output of a program in a file and to perform various operations on it. A stream is an abstraction that represents a device on which operations of input and output are performed. A stream can be represented as a source or destination of 259 CU IDOL SELF LEARNING MATERIAL (SLM)
characters of indefinite length depending on its usage. ofstream: This Stream class signifies the output file stream and is applied to create files for writing information to files ifstream: This Stream class signifies the input file stream and is applied for reading information from files fstream: This Stream class can be used for both read and write from/to files. 11.8KEY WORDS/ABBREVIATIONS C++ iostream Library: The C++ iostream library is an object-oriented implementation of this abstraction. Stream: A stream is a series of bytes whose value depends on the variable in which it is stored. Unformatted Input/output: Unformatted input/output is the simplest and most efficient form of input/output. 11.9LEARNINGACTIVITY 1. Write a program to store the information of about 5 students in a file and read the contents and print them on screen __________________________________________________________________________ __________________________________________________________________________ 2. Write a proper C++ statement with proper arguments that would be called to move the file pointer back by 2 bytes __________________________________________________________________________ __________________________________________________________________________ 260 CU IDOL SELF LEARNING MATERIAL (SLM)
11.10 UNIT END QUESTIONS (MCQ ANDDESCRIPTIVE) A. Descriptive Types Questions 1 Modify a program that opens a file and counts the whitespace-separated words in that file. 2 Create a program that counts the occurrence of a particular word in a file (use the string class’ operator ‘==’ to find the word). 3 Analyze a program to display a file a line at a time, waiting for the user to press the “Enter” key after each line 4 Create a vector<float> and put 25 floating-point numbers into it using a for loop. Display the vector. 5 Create a vector and<float> put 25 numbers into it as in the previous exercises. Now square each number and put the result back into the same location in the vector. Display the vector before and after the multiplication. B. Multiple Choice Questions 1. . Which header file is required to use file I/O operations? (a) <ifstream> (b) <ostream> (c) <fstream> (d) <iostream> 2. Which of the following is used to create an output stream? (a) ofstream (b) ifstream (c) iostream 261 CU IDOL SELF LEARNING MATERIAL (SLM)
(d) fsstream 3. Which of the following is used to create a stream that performs both input and output operations? (a) ofstream (b) ifstream (c) iostream (d) fstream 4. Which of the following is not used as a file opening mode? (a) ios::trunc (b) ios::binary (c) ios::in (d) ios::ate 5. By default, all the files in C++ are opened in _________ mode. (a) Text (b) Binary (c) ISCII (d) VTC Answers 1. (c), 2. (a), 3. (d), 4. (a), 5. (a) 11.11REFERENCES “Programming in C++” by M T Somashekara “Thinking in C++” by ECKEL The C++ Draft Standard by the International C++ Standards Committee (2013) Large-Scale C++ Software Design by John Lakos (1996) 262 CU IDOL SELF LEARNING MATERIAL (SLM)
http://www.cplusplus.com/reference/iostream/ http://www.cplusplus.com/reference/iostream/istream/getline/ https://www.learncpp.com/cpp-tutorial/186-basic-file-io/ https://www.thecrazyprogrammer.com/cpp-programs Analysis of Various I/O Methods for Large Datasets in C++ Kushaagra Moghe C++ STREAMS By Mike Toms Overload, 1(1):, April 1993 Bit-Oriented I/O with Templates By Mark Nelson, July 29, 2014 263 CU IDOL SELF LEARNING MATERIAL (SLM)
Search
Read the Text Version
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197
- 198
- 199
- 200
- 201
- 202
- 203
- 204
- 205
- 206
- 207
- 208
- 209
- 210
- 211
- 212
- 213
- 214
- 215
- 216
- 217
- 218
- 219
- 220
- 221
- 222
- 223
- 224
- 225
- 226
- 227
- 228
- 229
- 230
- 231
- 232
- 233
- 234
- 235
- 236
- 237
- 238
- 239
- 240
- 241
- 242
- 243
- 244
- 245
- 246
- 247
- 248
- 249
- 250
- 251
- 252
- 253
- 254
- 255
- 256
- 257
- 258
- 259
- 260
- 261
- 262
- 263
- 264
- 265