This Data Type Can Be Used to Create Files and Read Information From Them Into Memory.
C++ Files and Streams
So far, we have been using the iostream standard library, which provides cin and cout methods for reading from standard input and writing to standard output respectively.
This tutorial volition teach you how to read and write from a file. This requires some other standard C++ library called fstream, which defines 3 new data types −
Sr.No | Information Type & Description |
---|---|
1 | ofstream This information type represents the output file stream and is used to create files and to write information to files. |
two | ifstream This data type represents the input file stream and is used to read data from files. |
3 | fstream This data type represents the file stream by and large, and has the capabilities of both ofstream and ifstream which ways it can create files, write information to files, and read data from files. |
To perform file processing in C++, header files <iostream> and <fstream> must exist included in your C++ source file.
Opening a File
A file must be opened earlier you lot can read from it or write to it. Either ofstream or fstream object may be used to open a file for writing. And ifstream object is used to open a file for reading purpose only.
Following is the standard syntax for open() function, which is a member of fstream, ifstream, and ofstream objects.
void open(const char *filename, ios::openmode way);
Hither, the first argument specifies the name and location of the file to be opened and the second argument of the open up() fellow member role defines the mode in which the file should be opened.
Sr.No | Style Flag & Description |
---|---|
1 | ios::app Append manner. All output to that file to exist appended to the end. |
ii | ios::ate Open a file for output and movement the read/write control to the end of the file. |
3 | ios::in Open a file for reading. |
4 | ios::out Open up a file for writing. |
5 | ios::trunc If the file already exists, its contents volition exist truncated before opening the file. |
You lot can combine ii or more than of these values by ORing them together. For case if you desire to open up a file in write mode and want to truncate it in example that already exists, post-obit will be the syntax −
ofstream outfile; outfile.open("file.dat", ios::out | ios::trunc );
Similar way, you can open up a file for reading and writing purpose as follows −
fstream afile; afile.open("file.dat", ios::out | ios::in );
Closing a File
When a C++ plan terminates it automatically flushes all the streams, release all the allocated retentiveness and close all the opened files. Merely it is always a good practice that a programmer should close all the opened files before program termination.
Post-obit is the standard syntax for close() function, which is a fellow member of fstream, ifstream, and ofstream objects.
void close();
Writing to a File
While doing C++ programming, you write data to a file from your program using the stream insertion operator (<<) just as you use that operator to output information to the screen. The only deviation is that you employ an ofstream or fstream object instead of the cout object.
Reading from a File
You read information from a file into your program using the stream extraction operator (>>) just as you use that operator to input information from the keyboard. The only difference is that you use an ifstream or fstream object instead of the cin object.
Read and Write Example
Following is the C++ program which opens a file in reading and writing mode. After writing information entered by the user to a file named afile.dat, the program reads information from the file and outputs it onto the screen −
#include <fstream> #include <iostream> using namespace std; int chief () { char data[100]; // open a file in write mode. ofstream outfile; outfile.open up("afile.dat"); cout << "Writing to the file" << endl; cout << "Enter your proper name: "; cin.getline(data, 100); // write inputted information into the file. outfile << data << endl; cout << "Enter your age: "; cin >> information; cin.ignore(); // again write inputted data into the file. outfile << data << endl; // close the opened file. outfile.shut(); // open a file in read mode. ifstream infile; infile.open up("afile.dat"); cout << "Reading from the file" << endl; infile >> information; // write the data at the screen. cout << data << endl; // once again read the information from the file and brandish it. infile >> data; cout << information << endl; // close the opened file. infile.close(); return 0; }
When the above code is compiled and executed, information technology produces the following sample input and output −
$./a.out Writing to the file Enter your name: Zara Enter your age: 9 Reading from the file Zara ix
Higher up examples make use of additional functions from cin object, like getline() function to read the line from outside and ignore() function to ignore the extra characters left by previous read statement.
File Position Pointers
Both istream and ostream provide member functions for repositioning the file-position pointer. These member functions are seekg ("seek get") for istream and seekp ("seek put") for ostream.
The argument to seekg and seekp normally is a long integer. A second argument tin can be specified to indicate the seek direction. The seek direction can be ios::beg (the default) for positioning relative to the offset of a stream, ios::cur for positioning relative to the electric current position in a stream or ios::terminate for positioning relative to the end of a stream.
The file-position arrow is an integer value that specifies the location in the file as a number of bytes from the file'due south starting location. Some examples of positioning the "get" file-position arrow are −
// position to the nth byte of fileObject (assumes ios::beg) fileObject.seekg( northward ); // position n bytes forward in fileObject fileObject.seekg( n, ios::cur ); // position n bytes back from end of fileObject fileObject.seekg( n, ios::cease ); // position at terminate of fileObject fileObject.seekg( 0, ios::end );
Useful Video Courses
Video
Video
Video
Video
Video
Video
Source: https://www.tutorialspoint.com/cplusplus/cpp_files_streams.htm
0 Response to "This Data Type Can Be Used to Create Files and Read Information From Them Into Memory."
Post a Comment