Updated April 3, 2023
Introduction of C++ write file
The following article provides an outline for the C++ write file. In the C++ language, write is one of the types of a member function of the outputstream(ostream); it can be inherited by using ofstream it’s a special type of binary files is also one of the read member functions, which is to be like istream its inherited by using ifstream generally objects of the c++ can be classified into the input stream and outputstream sometimes the object of the particular class attributes like fstream operates using both kinds of stream functions in its prototypes has been followed by using write type because of its increases the memory blocks and sizes.
Syntax
The c++ write is used to print the datas to the files using some stream operators like insertion operator(<<) likewise the operators are used to write the output datas to the user screen. It has its own syntax and properties for utilizing the applications.
#include <iostream> //include the header files like input-output streams
#include <fstream> //include the filestreamobject as the header files
using namespace std;
main()
{
ofstream o; //ofstream is the class for fstream package
o.open("filename with extension"); //open is the method of ofstream
o << "print this file data to the user screen"; // << operator which is used to print the file informations in the screen
o.close();
---some logics depends upon the requirement---
}
How to write a file in C++?
Whenever we want to write the datas using file stream functions we used basic insertion operators like <<its just use as an operator to print the output datas to the user screen. The basic difference between this write files and other default file functions like “cout” which has the default keyword to print whatever the printing datas of the user requirement which is to be displayed on the screen. But instead of that, we used to write the datas to the screen using the << operator with the help of file stream objects like fstream or ofstream through these object instances we may print the output results in the user screen. In Generally c++ provides different classes for to perform the input and output characters from the specific files like ofstream the stream class which has to be written on the files,ifstream which has been read from the files, and finally we used fstream class for accessing both read and write from the files. Also, these classes are derived directly or indirectly from the classes using istream and ostream. We already used objects whose types are the classes using cin as an object of istream class and cout is the object of ostreamclass. We have already been used as the classes that are related to our file streams and in fact, we can use our file stream operations in the same way we have already used to the cin and cout operations with only the differences that we associated with these streams using physical files. The operations are most generally performed on the specific objects are one of the above specific classes is associated with the real files this procedure is known as an open file. It is represented within the program by using the stream classes like above istream, ostream, and fstream. Also, any of the input and output operations performed on the specific stream objects that will be applied to the physical files it’s already associated with the open file. We use the open as the keyword like open(filename, mode) it’s a syntax where filename is mentioned as the string format that represents the name of the file its to be opened and mode as the optional parameter with a combination of the different flags like ios::in it can be opened for only input operations and ios::out its opened for output operations. The ios::binary is used to open the binary mode, ios::ate it sets the initial position at the end of the file, and also the flag is not set at the initial position as well as the beginning of the file. In ios::app all the output operations are performed at the end of the file and it’s appending at the contents to the current contents of the files. The finally we use the file name as the ios::trunc string format using this format the file is opened for specific output operations and it has already existed as the previous contents also deleted and replaced using by the new ones.
Examples
Given below are the examples:
Example #1
Code:
#include <fstream>
#include <iostream>
using namespace std;
int main () {
char in[80];
ofstream o;
o.open("C:\\TURBOC3\\BIN\\PROJECT\\Example.txt");
cout<<"Welcome To My Domain:" <<endl;
cout<< "Please Enter the name: ";
cin.getline(in, 100);
o << in <<endl;
cout<< "Please Enter the pincode: ";
cin>> in;
cin.ignore();
o << in <<endl;
o.close();
ifstream ifs;
string s;
ifs.open("C:\\TURBOC3\\BIN\\PROJECT\\Example.txt");
cout<< "Reading data from the text file:" <<endl;
while (getline (ifs,s))
{
cout<< s <<endl;
}
ifs.close();
return 0;
}
Output:
Example #2
Code:
#include <iostream>
#include <fstream>
using namespace std;
int main(){
char t[300];
fstream f;
f.open ("C:\\TURBOC3\\BIN\\PROJECT\\Example.txt", ios::out | ios::in );
cout<< "Welcome To My Domain." <<endl;
cin.getline(t, sizeof(t));
f << t <<endl;
f >> t;
cout<< t <<endl;
f.close();
return 0;
}
Output:
Example #3
Code:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
fstream f;
string s;
f.open("C:\\TURBOC3\\BIN\\PROJECT\\Example.txt", ios::trunc | ios::out | ios::in);
while (f) {
getline(cin, s);
if (s == "-1")
break;
f << s <<endl;
}
f.seekg(0, ios::beg);
while (f) {
getline(f, s);
cout<< s <<endl;
cout<< "Have a Nice day user" <<endl;
}
f.close();
return 0;
}
Output:
The above examples are the basics of the c++ write concepts for reading and writing the files in line by line using some default methods like open(),seekg(), and getline().
Conclusion
The C++ writes file streams it operates the file streams that are associated with an internal buffer object of the type like streambuf etc because the data transfer is happened using buffer type each object is represented using separate individual memory blocks its used for an intermediary between the stream and physical files.
Recommended Articles
This is a guide to C++ write file. Here we discuss How to write a file in C++ and Examples along with the codes and outputs. You may also have a look at the following articles to learn more –