to create an input file object, you would use what kind of type?
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 will teach you how to read and write from a file. This requires another standard C++ library called fstream, which defines three new data types −
| Sr.No | Information Type & Description |
|---|---|
| ane | ofstream This data type represents the output file stream and is used to create files and to write information to files. |
| ii | ifstream This data type represents the input file stream and is used to read information from files. |
| 3 | fstream This information type represents the file stream generally, and has the capabilities of both ofstream and ifstream which means it tin create files, write data to files, and read data from files. |
To perform file processing in C++, header files <iostream> and <fstream> must be included in your C++ source file.
Opening a File
A file must be opened before y'all tin 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 just.
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 manner);
Here, the first statement specifies the name and location of the file to exist opened and the 2d statement of the open() fellow member part defines the fashion in which the file should be opened.
| Sr.No | Style Flag & Description |
|---|---|
| 1 | ios::app Suspend style. All output to that file to be appended to the end. |
| 2 | 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 a file for writing. |
| 5 | ios::trunc If the file already exists, its contents will be truncated before opening the file. |
You can combine 2 or more of these values by ORing them together. For example if you lot want to open up a file in write mode and desire to truncate it in case that already exists, following will be the syntax −
ofstream outfile; outfile.open("file.dat", ios::out | ios::trunc ); Similar way, you tin can open 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++ program terminates it automatically flushes all the streams, release all the allocated memory and close all the opened files. But it is e'er a good practice that a programmer should shut all the opened files before program termination.
Following is the standard syntax for close() office, which is a fellow member of fstream, ifstream, and ofstream objects.
void close();
Writing to a File
While doing C++ programming, yous write information to a file from your plan using the stream insertion operator (<<) merely equally you use that operator to output information to the screen. The only departure is that you use 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 equally yous 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 way. Subsequently 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 main () { char data[100]; // open a file in write mode. ofstream outfile; outfile.open("afile.dat"); cout << "Writing to the file" << endl; cout << "Enter your name: "; cin.getline(data, 100); // write inputted data into the file. outfile << data << endl; cout << "Enter your age: "; cin >> data; cin.ignore(); // again write inputted data into the file. outfile << data << endl; // shut the opened file. outfile.close(); // open up a file in read mode. ifstream infile; infile.open("afile.dat"); cout << "Reading from the file" << endl; infile >> data; // write the information at the screen. cout << data << endl; // over again read the data from the file and brandish it. infile >> data; cout << data << endl; // close the opened file. infile.shut(); return 0; } When the to a higher place 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 historic period: nine Reading from the file Zara ix
Above examples make use of additional functions from cin object, like getline() role 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 go") for istream and seekp ("seek put") for ostream.
The argument to seekg and seekp ordinarily is a long integer. A 2d argument can be specified to indicate the seek direction. The seek management can exist ios::beg (the default) for positioning relative to the beginning of a stream, ios::cur for positioning relative to the current position in a stream or ios::finish for positioning relative to the finish 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'southward starting location. Some examples of positioning the "get" file-position arrow are −
// position to the nth byte of fileObject (assumes ios::beg) fileObject.seekg( n ); // position northward bytes forward in fileObject fileObject.seekg( n, ios::cur ); // position n bytes back from stop of fileObject fileObject.seekg( n, ios::end ); // position at cease of fileObject fileObject.seekg( 0, ios::finish );
Useful Video Courses
Video
Video
Video
Video
Video
Video
Source: https://www.tutorialspoint.com/cplusplus/cpp_files_streams.htm
0 Response to "to create an input file object, you would use what kind of type?"
Post a Comment