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 yous how to read and write from a file. This requires some other standard C++ library chosen fstream, which defines three new information types −

Sr.No Data Blazon & Description
1

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 data from files.

three

fstream

This data type represents the file stream generally, and has the capabilities of both ofstream and ifstream which means it can create files, write information to files, and read information 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 exist opened before yous tin can read from it or write to information technology. 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 but.

Post-obit is the standard syntax for open() role, which is a member of fstream, ifstream, and ofstream objects.

void open(const char *filename, ios::openmode mode);        

Here, the first argument specifies the name and location of the file to exist opened and the 2d argument of the open() fellow member function defines the mode in which the file should be opened.

Sr.No Mode Flag & Clarification
1

ios::app

Append mode. All output to that file to be appended to the end.

2

ios::ate

Open a file for output and move 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 volition be truncated before opening the file.

Yous can combine two or more of these values by ORing them together. For example if you want to open a file in write fashion and desire to truncate it in example that already exists, following will be the syntax −

ofstream outfile; outfile.open up("file.dat", ios::out | ios::trunc );        

Similar way, you tin open a file for reading and writing purpose as follows −

fstream  afile; afile.open up("file.dat", ios::out | ios::in );        

Closing a File

When a C++ program terminates information technology automatically flushes all the streams, release all the allocated memory and close all the opened files. Simply it is always a good practice that a programmer should close all the opened files earlier programme termination.

Following is the standard syntax for shut() role, which is a 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 lot use that operator to output information to the screen. The only difference is that y'all 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 (>>) merely as you use that operator to input information from the keyboard. The only difference is that you lot use an ifstream or fstream object instead of the cin object.

Read and Write Case

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 primary () {    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 information into the file.    outfile << data << endl;     cout << "Enter your age: ";     cin >> data;    cin.ignore();        // once more write inputted data into the file.    outfile << data << endl;     // close the opened file.    outfile.shut();     // open a file in read manner.    ifstream infile;     infile.open up("afile.dat");       cout << "Reading from the file" << endl;     infile >> data;      // write the data at the screen.    cout << data << endl;        // once more read the data from the file and brandish information technology.    infile >> data;     cout << information << endl;      // shut the opened file.    infile.close();     return 0; }        

When the above lawmaking is compiled and executed, it 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        

Above examples brand 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 argument.

File Position Pointers

Both istream and ostream provide member functions for repositioning the file-position pointer. These fellow member functions are seekg ("seek become") for istream and seekp ("seek put") for ostream.

The argument to seekg and seekp normally is a long integer. A second argument can be specified to indicate the seek direction. The seek direction tin be ios::beg (the default) for positioning relative to the beginning of a stream, ios::cur for positioning relative to the electric current position in a stream or ios::end for positioning relative to the end of a stream.

The file-position pointer is an integer value that specifies the location in the file as a number of bytes from the file'south starting location. Some examples of positioning the "go" file-position pointer are −

// position to the nth byte of fileObject (assumes ios::beg) fileObject.seekg( n );  // position n bytes forrad in fileObject fileObject.seekg( north, ios::cur );  // position n bytes back from end of fileObject fileObject.seekg( n, ios::end );  // position at stop of fileObject fileObject.seekg( 0, ios::end );        

Useful Video Courses


C++ Online Training

Video

Learn C++ Pointers with Visual Studio 2017

Video

Modern OpenGL C++ 3D Game &amp; 3D Rendering

Video

Cocos2d-x v3 C++ - Beginning Game Development

Video

C++ Development - The Complete Coding Guide

Video

Tic-Tac-Toe Clone - The Complete Cocos2d-x C++ Game Course

Video

turnercomed1944.blogspot.com

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."

Postar um comentário

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel