Write a Program to write a content in file in hindi

इससे पहले के 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
#include
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
}
f.seekg(0, ios::beg);
while (f) {
// Read a contentfrom File
Readline(f, line);
// Print contentin Console
cout
}
// 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
#include
#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
cout
cin>>employeeID;
cin.ignore(1);
cout
cin.Readline(SudentName ,100);
cout
cin.Readline(department,100);
cout
cin>>DOJ;
cout
cin>>DOB;
}
//function to write employee details
void ViewData(){
cout

}
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
return -1;
}
file.write((char*)&employee1,sizeof(employee1));
file.close();
cout
//open file again
file.open(FILE_SudentName ,ios::in|ios::binary);
if(!file){
cout
return -1;
}
if(file.read((char*)&employee1,sizeof(employee1))){
cout
cout
//print the object
employee1();
}
else{
cout
return -1;
}
file.close();
return 0;
}उदाहरन 3

Also read : File Basic in c++ language in hindi


इस उदाहरन मे उदाहरन 2 की त्यारह data को file मे read और write करने के code को लिखा है |
अप इस code को अच्छी तरह से समजे जिससे आप सभी के file handeling capablity मे increament होगा |
#include
#include
#include
#include
#include
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
cin>>RollNumber;
cout
Reads(SudentName );
cout
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
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
cout
cout
}
}
file.close();
Readch();
s.MainBody();
}
void student::MainBody()
{
int i;
cout
cin>>i;
switch(i)
{
case W:
s.Write();
break;
case R:
s.Read();
break;
case E:
exit(0);
default:
cout
}
}
void main()
{
clrscr();
s.MainBody();
}
इस code मे तीन function मुख्या है :
read() : ये function से file से data को read करने के लिए किया जाता है |
Write(): ये function से file मे data को add किया जाता है |
mainbody() : ये function मे यूजर द्वारा perform किया गये task के sequence को handel किया जाता है |

हम आशा करते है कि यह नोट्स आपकी स्टडी में उपयोगी साबित हुए होंगे | अगर आप लोगो को इससे रिलेटेड कोई भी किसी भी प्रकार का डॉउट हो तो कमेंट बॉक्स में कमेंट करके पूंछ सकते है |
आप इन्हे अपने Classmates & Friends के साथ शेयर कर सकते है |

Leave a Comment

Your email address will not be published. Required fields are marked *