Basic Operation On Text File In C++ - learnit

Home Top Ad

Post Top Ad

Sunday, May 31, 2020

Basic Operation On Text File In C++

C Programming How to Use switch Statement

Basic Operation On Text File in C++

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.


The fstream Library

The fstream library provides C++ programmers with three classes for working with files. These classes include:


-ofstream- This class represents an output stream. It's used for creating files and writing information to files.


-ifstream- This class represents an input stream. It's used for reading information from data files.


-fstream- This class generally represents a file stream. It comes with ofstream/ifstream capabilities. This means it's capable of creating files, writing to files, reading from data files.


How to Open Files

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.


Syntax

open(File_Name,Mode);

The file_name parameter denotes the name of the file to open.

The mode parameter is optional. It can take any of the following values:

Value Description
ios::out It opens the file for a write.
ios::in It opens the file for a read.
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::trunk If a file exists, the file elements should be truncated prior to its opening.

Example1

#include<iostream>
#include<fstream>
using namespace std;
class BiData{
int id,age,phone;
char first[20];
char last[20];
public: void writedata(int id,char first[20],char last[20],int age,int phone)
{
ofstream of;
of.open("Hello.bat",ios::out|ios::binary|ios::app);
of.write((char *)&id,sizeof(id));
of.write((char *)&first,sizeof(first));
of.write((char *)&last,sizeof(last));
of.write((char *)&age,sizeof(age));
of.write((char *)&phone,sizeof(phone));
of.close();
cout<<"Success store Data";
}
};
int main()
{
int i;
BiData obj;
int id,age,phone;
char frist[20],last[20];
for (i=0;i<=2;i++){
cout<<"Please input Number ID";cin>>id ;
cout<<"Please input First Name";cin>>frist;
cout<<"Please input Last Name";cin>>last;
cout<<"Please input age";cin>>age;
cout<<"Please input Phone number";cin>>phone;
obj.writedata(id,frist,last,age,phone);
}
}

Example 2:

#include<iostream> //clude the iostream header file in the program to use its functions
#include<fstream> //Include the fstream header file in the program to use its classes
using namespace std; //Include the std namespace in our code to use its classes without calling it.
class person{
int id,age,phone;
char first[20],last[20];
public: void adddata(){
cout<<"Please input Number ID";cin>>id;
cout<<"Please input First Name";cin>>first;
cout<<"Please input Last Name";cin>>last;
cout<<"Please input age";cin>>age;
cout<<"Please input Phone Number";cin>>phone;
}
void showdata(){
cout<<"ID" <<"\t" <<"First Name" <<"\t" <<"Last Name" <<"\t" <<"age" <<"\t" <<"Phnoe Number" <<"\t";
cout<<id <<"\t" <<first <<"\t" <<last <<"\t" <<age <<"\t" <<phone <<"\t";
}
};
void writedata()
{
person obj;
ofstream os;
os.open("person.dat",ios::binary|ios::app);
obj.adddata();
os.write((char *)&obj,sizeof(obj));
os.close();
obj.showdata();
}
int main() //Call the main() function. The program logic should go within its body.
{
writedata();

Please Watching My Video is Below

No comments:

Post a Comment

Post Top Ad