Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 4

Program 1 Sample program

#include<fstream.h> #include<iostream.h> int main() { Ofstream filename; filename.open(student.txt); filename<<Hello world; filename.close();

Program 2 Program to open a file


#include<fstream.h> #include<iostream.h> int main() { Ofstream filename(new.txt); if(filename.is!open()) { cout<<"he file is opened<<endl; else { cout<<"he file is not opened<<endl;

filename<<it is a new text file; filename.close();

Program 3 Program to write a file


#include<fstream.h> #include<iostream.h> int main() { Ofstream filename(studentinfo.txt); cout<<#nter stud$%& 'ame& (ar)s<<endl; cout<<*ress cntrl+, to -uit<<endl; int id; strin. name; int mar)s; while(cin>>id>>name>>mar)s) { filename<<id<</ 0; <<name<</ 0; <<mar)s<<endl;

Program 4 Program to read a file


#include<fstream.h> #include<iostream.h> int main() { ifstream filename(studentinfo.txt); int id; strin. name; int mar)s; while(filename>>id>>name>>mar)s) { cout<<id<</10 <<name<</10 <<mar)s<</1/<<endl;

Program 5 Program to get information from a file and display the information with the given choice (Initially you have to create a text file named objects as chair 0 pen 0 pencil 0 Alcohol - ! fuel -"0 #ruits !! medicines "!$ %xample&
#include<fstream.h> #include<iostream.h> int .etwhatuwant(); 2oid displa3items(int x); int main() { int whatuwant; whatuwant 4 .etwhatuwant(); while(whatuwant546) { switch(whatuwant) { case 78 displa3items(7); 9rea); case :8 displa3items(:); 9rea); case ;8 displa3items(;); 9rea); whatuwant 4 .etwhatuwant();

11.etwhatuwant function int .etwhatuwant() { int choice; cout<<7<plain items<<endl; cout<<:<harmful items<<endl; cout<<;<helpful items<<endl; cout<<6<plain items<<endl; cin>>choice; return choice;

11displa3item function 2oid displa3items(int x) { ifstream filename(o9=ects.txt); strin. name; dou9le 2alue; if(x447) { while(filename>>name>>2alue) { if(2alue44>) { cout<<name<<//<<num9er<<endl;

if(x44:) { while(filename>>name>>2alue) { if(2alue<>) { cout<<name<<//<<num9er<<endl;

if(x44:) { while(filename>>name>>2alue) { if(2alue>>) { cout<<name<<//<<num9er<<endl;

You might also like