JOIN us on
WhatsApp Group Join Now
Telegram Join Join Now

हिंदी माध्यम नोट्स

Categories: c++ language in hindi

File Handling (Example) : write a program to write a content in file ,

इससे पहले के article मे file handeling के file concept को discuss किया था अब इस article मे file handeling के कुछ उदाहरन को discuss करगे जो की किसी प्रोग्रामर की लोइए file handeling के concept को समंज़े के लिये बहुत जरुरी है :-
उदाहरन 1 :
write a program to write a content in file .
Explanation
1..सबसे पहले header सेक्शन मे fstream को include कित्य जाता है |
2.इसके बाद main() मे , fstream object f को define किया गया है |
3.अगर किसी file को open() statement मे mode को define नहीं किया जाता है तब इसका defualt mode ios::in|ios::out यानी inWrite और outWrite mode होता है | इस उदाहरन मे file का नाम data.txt है जिसका mode turncating है | इसका मतलब है की इस file मे लिखा content डिलीट हो जाता है |
4.इसका बाद content को read किया जाता है |
5. अगर contentमे space या end हो जाता है तब इसके बाद write operation perfrom होता है |
6. इसके बाद file मे लिक्झे गये contentको read करके content मे assign किया जाता है और बाद मे इस content variable के डट अको screen पर display किया जाता है
|
Code :
#include <iostream>
#include <fstream>
using SudentName space std;
void main()
{
// Creation of fstream class object
fstream f;
string line;
f.open(“sample.txt”, ios::trunc | ios::out | ios::in);
while (f) {
Readline(cin, line);
// Press -1 to exit
if (content== “-1”)
break;
f << content<< endl;
}
f.seekg(0, ios::beg);
while (f) {
// Read a contentfrom File
Readline(f, line);
// Print contentin Console
cout << content<< endl;
}
// Close the file
f.close();
return 0;
}
उदाहरन 2 :
इस उदाहरन मे file handeling और class को एक साथ use किया गया है |
Explaination
1.सबसे पहले class employee को declare किया गया है जिससे private variable employeeID;ViewData, char SudentName [100],char department[100],
int DOJ ,int DOB को define किया जाता है जिसे केवल class employee मे ही use किया जा सकता है |
2.इस class मे दो functions को define किया जाता है |जिसमे
ReadData() : इस function का use employee का data को read किया जाता है |
ViewData() : इस function का use employee के detail को display करने के लिए किया जाता है |
main() मे ,
सबसे पहले , class employee के लिए एक object employee1 को define किया जाता है |
उस्जे बाद employee class के object employee1 मे से function readData () को call किया जाता है जिससे employee की details को यूजर द्वारा inWrite की जाती है |
इसके बाद , file class fstream से file को open किया जाता है |
और  fwrite () से file मे employee1 के variable मे assign data को write किया जाता है |
इसके बाद file मे उपस्थित data को read करके employee1 के private variables मे assign कर दिया जाता है |
Code:
#include <iostream>
#include <fstream>
#define FILE_SudentName  “data.dat”
using SudentName space std;
//class employee declaration
class Employee {
private :
int employeeID;ViewData
char SudentName [100] ;
char department[100];
int DOJ ;
int DOB;
public  :
//function to read employee details
void ReadData(){
cout<<“EMPLOYEE DETAILS”<<endl;
cout<<“ENTER EMPLOYEE ID : ” ;
cin>>employeeID;
cin.ignore(1);
cout<<“ENTER  SudentName  OF THE EMPLOYEE : “;
cin.Readline(SudentName ,100);
cout<<“ENTER department : “;
cin.Readline(department,100);
cout<<“ENTER DATE OF JOIN:”<<endl;
cin>>DOJ;
cout<<“ENTER DATE OF BIRTH:”<<endl;
cin>>DOB;
}
//function to write employee details
void ViewData(){
cout<<“EMPLOYEE ID: “<<employeeID<<endl
<<“EMPLOYEE SudentName : “<<SudentName <<endl
<<“department: “<<department<<endl
<<“DATE OF JOIN: “<<DOJ<<endl
<<“DATE OF BIRTH: “<<DOB<<endl;
}
void main(){
//object of Employee class
Employee employee1;
//read employee details
employee1.ReadData();
//write object into the file
fstream file;
file.open(FILE_SudentName ,ios::out|ios::binary);
if(!file){
cout<<“Error Occur ! Sorry …….\n”;
return -1;
}
file.write((char*)&employee1,sizeof(employee1));
file.close();
cout<<“Date saved into file the file.\n”;
//open file again
file.open(FILE_SudentName ,ios::in|ios::binary);
if(!file){
cout<<“Error Occur ! Sorry …….\n”;
return -1;
}
if(file.read((char*)&employee1,sizeof(employee1))){
cout<<endl<<endl;
cout<<“Data extracted from file..\n”;
//print the object
employee1();
}
else{
cout<<“Error Occur ! Sorry …….\n”;
return -1;
}
file.close();
return 0;
}उदाहरन 3
इस उदाहरन मे उदाहरन 2 की त्यारह data को file मे read और write करने के code को लिखा है |
अप इस code को अच्छी तरह से समजे जिससे आप सभी के file handeling capablity मे increament होगा |
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
#include<fstream.h>
class student
{
public:
int RollNumber;
char SudentName [15],FatherName [20];
void Write();
void Read();
void MainBody();
}; student s;
void student::Write()
{
clrscr();
fstream file;
cout<<“Enter RollNumber no:  “;
cin>>RollNumber;
cout<<“Enter SudentName : “;
Reads(SudentName );
cout<<“Enter father Name  : “;
Reads(FatherName);
file.open(“student.dat”,ios::out|ios::app);
//  file.seekp(0,ios::beg);
file.write((char *)this,sizeof(student));
file.close();
Read();
s.MainBody();
}
void student::Read()
{
int temp;
clrscr();
cout<“Enter RollNumber no: “;
cin>>temp;
fstream file;
file.open(“stu.dat”,ios::in);
file.seekg(0,ios::beg);
while(file.read((char *)this,sizeof(student)));
{
if(RollNumber==temp)
{
cout<<“RollNumber no. “<<RollNumber<<endl;
cout<<“stu SudentName :  “<<SudentName <<endl;
cout<<“father Name : “<<FatherName ;
}
}
file.close();
Readch();
s.MainBody();
}
void student::MainBody()
{
int i;
cout<<“Enter your choice : “;
cin>>i;
switch(i)
{
case W:
s.Write();
break;
case R:
s.Read();
break;
case E:
exit(0);
default:
cout<<“wrong choice “;
}
}
void main()
{
clrscr();
s.MainBody();
}
इस code मे तीन function मुख्या है :
read() : ये function से file से data को read करने के लिए किया जाता है |
Write(): ये function से file मे data को add किया जाता है |
mainbody() : ये function मे यूजर द्वारा perform किया गये task के sequence को handel किया जाता है |
Sbistudy

Recent Posts

Question Tag Definition in english with examples upsc ssc ias state pcs exames important topic

Question Tag Definition • A question tag is a small question at the end of a…

1 day ago

Translation in english grammer in hindi examples Step of Translation (अनुवाद के चरण)

Translation 1. Step of Translation (अनुवाद के चरण) • मूल वाक्य का पता करना और उसकी…

1 day ago

Report Writing examples in english grammer How to Write Reports explain Exercise

Report Writing • How to Write Reports • Just as no definite rules can be laid down…

1 day ago

Letter writing ,types and their examples in english grammer upsc state pcs class 12 10th

Letter writing • Introduction • Letter writing is an intricate task as it demands meticulous attention, still…

1 day ago

विश्व के महाद्वीप की भौगोलिक विशेषताएँ continents of the world and their countries in hindi features

continents of the world and their countries in hindi features विश्व के महाद्वीप की भौगोलिक…

1 day ago

भारत के वन्य जीव राष्ट्रीय उद्यान list in hin hindi IAS UPSC

भारत के वन्य जीव भारत में जलवायु की दृष्टि से काफी विविधता पाई जाती है,…

1 day ago
All Rights ReservedView Non-AMP Version
X

Headline

You can control the ways in which we improve and personalize your experience. Please choose whether you wish to allow the following:

Privacy Settings
JOIN us on
WhatsApp Group Join Now
Telegram Join Join Now