R Programming
R Programming Tutorial PDF: Learn Basics (Download Now)
$20.20 $9.99 for today 4.6 (125 ratings) Key Highlights of R Programming Tutorial PDF 383+ pages...
Files store data permanently in a storage device. With file handling, the output from a program can be stored in a file. Various operations can be performed on the data while in the file.
A stream is an abstraction of a device where input/output operations are performed. You can represent a stream as either a destination or a source of characters of indefinite length. This will be determined by their usage. C++ provides you with a library that comes with methods for file handling. Let us discuss it.
In this c++ tutorial, you will learn:
The fstream library provides C++ programmers with three classes for working with files. These classes include:
The following image makes it simple to understand:
To use the above classes of the fstream library, you must include it in your program as a header file. Of course, you will use the #include preprocessor directive. You must also include the iostream header file.
Before performing any operation on a file, you must first open it. If you need to write to the file, open it using fstream or ofstream objects. If you only need to read from the file, open it using the ifstream object.
The three objects, that is, fstream, ofstream, and ifstream, have the open() function defined in them. The function takes this syntax:
open (file_name, mode);
| Value | Description |
| ios:: app | The Append mode. The output sent to the file is appended to it. |
| ios::ate | It opens the file for the output then moves the read and write control to file's end. |
| ios::in | It opens the file for a read. |
| ios::out | It opens the file for a write. |
| ios::trunk | If a file exists, the file elements should be truncated prior to its opening. |
It is possible to use two modes at the same time. You combine them using the | (OR) operator.
#include <iostream>
#include <fstream>
using namespace std;
int main() {
fstream my_file;
my_file.open("my_file", ios::out);
if (!my_file) {
cout << "File not created!";
}
else {
cout << "File created successfully!";
my_file.close();
}
return 0;
}
Output:
Here is a screenshot of the code:
Code Explanation:
Once a C++ program terminates, it automatically
However, as a programmer, you should learn to close open files before the program terminates.
The fstream, ofstream, and ifstream objects have the close() function for closing files. The function takes this syntax:
void close();
You can write to file right from your C++ program. You use stream insertion operator (<<) for this. The text to be written to the file should be enclosed within double-quotes.
Let us demonstrate this.
#include <iostream>
#include <fstream>
using namespace std;
int main() {
fstream my_file;
my_file.open("my_file.txt", ios::out);
if (!my_file) {
cout << "File not created!";
}
else {
cout << "File created successfully!";
my_file << "gtupapers";
my_file.close();
}
return 0;
}
Output:
Here is a screenshot of the code:
Code Explanation:
You can read information from files into your C++ program. This is possible using stream extraction operator (>>). You use the operator in the same way you use it to read user input from the keyboard. However, instead of using the cin object, you use the ifstream/ fstream object.
#include <iostream>
#include <fstream>
using namespace std;
int main() {
fstream my_file;
my_file.open("my_file.txt", ios::in);
if (!my_file) {
cout << "No such file";
}
else {
char ch;
while (1) {
my_file >> ch;
if (my_file.eof())
break;
cout << ch;
}
}
my_file.close();
return 0;
}
Output:
No such file
Here is a screenshot of the code:
Code Explanation:
$20.20 $9.99 for today 4.6 (125 ratings) Key Highlights of R Programming Tutorial PDF 383+ pages...
In this tutorial of difference between AR and VR, we will discuss the key difference between...
How to Export Data from R In this tutorial, we will learn how to export data from R environment to different...
You can use the geometric object geom_boxplot() from ggplot2 library to draw a boxplot() in R....
Typing Tutor is a software which helps you to improve your typing skills by taking lessons,...
What is Concurrency or Single Core? In Operating Systems, concurrency is defined as the ability of a...