We will use the header file <fstream> which defines three new data types:
Before you can read from or write to a file, it must first be opened. To open a file for writing, use either ofstream or fstream. And the ifstream object is used to open a file solely for reading purposes.
We create/open a file by specifying the file’s new path and mode of operation.
File creation syntax:
void open(const char *filename, ios::openmode mode);
for instance:
FilePointer.open("Path",ios::mode);
Mode Flag:
Mode Flag | Description |
---|---|
ios::app | Append mode. All output to that file is to be appended to the end. |
ios::ate | Open a file for output and move the read/write control to the end of the file. |
ios::in | Open a file for reading. |
ios::out | Open a file for writing. |
ios::trunc | If the file already exists, its contents will be truncated before opening the file. |
#include<iostream>
#include<conio>
#include <fstream>
using namespace std;
int main()
{
fstream st; //Creating object of fstream class
st.open("C:\File1.txt",ios::out); // Creating new file
if(!st) // Checking whether file exist
{
cout<<"File creation failed";
}
else
{
cout<<"New file created";
st.close(); // Closing file
}
getch();
return 0;
}
The stream extraction operator (>>) is used to read data from a file into our program, just as it is used to input data from the keyboard. The only difference is that instead of the cin object, we use an ifstream or fstream object.
#include <iostream>
#include<conio>
#include <fstream>
using namespace std;
int main()
{
fstream st; // Creating object of fstream class
st.open("C:\File1.txt",ios::in); // Creating new file
if(!st) // Checking whether file exist
{
cout<<"No such file";
}
else
{
char ch;
while (!st.eof())
{
st >>ch; // Reading from file
cout << ch; // Message Read from file
}
st.close(); // Closing file
}
getch();
return 0;
}
In C++ programming, we use the stream insertion operator (<<) to write information to a file from your program, just as we use that operator to output information to the screen. The only difference is that instead of the cout object, we use an ofstream or fstream object.
#include <iostream>
#include<conio>
#include <fstream>
using namespace std;
int main()
{
fstream st; // Creating object of fstream class
st.open("C:\File1.txt",ios::out); // Creating new file
if(!st) // Checking whether file exist
{
cout<<"File creation failed";
}
else
{
cout<<"New file created";
st<<"Hello"; // Writing to file
st.close(); // Closing file
}
getch();
return 0;
}
It is done by FilePointer.close(). Just the way we did in the previous examples.
st.close(); // Closing file
There are a few important functions that can be used with file streams:
seekg( ):
seekp():
tellp():
tellg():
Note: also read about Exception Handling in C++
Please follow me to read my latest post on programming and technology if you like my post.
https://www.instagram.com/coderz.py/
https://www.facebook.com/coderz.py
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…
Given an integer A. Compute and return the square root of A. If A is…
Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…
A heap is a specialized tree-based data structure that satisfies the heap property. It is…
What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…