SlideShare a Scribd company logo
1 of 8
#include<iostream.h>
#include<fstream.h>
#include<math.h>
void main()
{
clrscr();
ofstream fout("Myfile");
fout<<"Ganesh";
fout.close();
ifstream fin("Myfile");
char ch;
while(fin)
{
fin.get(ch);
cout<<ch;
}
fin.close();
getch();
}
52. Write a program that will create a data file containing the list of telephone numbers
and name. Use a class object to store each set of data.
/*
* To write the complex data types like array, structure or
* classes, we have already discussed that only one method
* for read and one method for write is used :
* These are - fout.write((char *)&obj, sizeof(obj));
* and fin.read((char *)&newObj, sizeof(newObj));
*/
#include<fstream.h>
#include<conio.h>
#include<iostream.h>
#include<fstream.h>
class tel
{
private:
char *name, *number;
public:
void setdata()
{
cout<<"Enter Name and number"<<endl;
cin>>name>>number;
}
void display()
{
cout<<"Name : "<<name<<endl;
cout<<"Number : "<<number<<endl;
}
};
void main()
{
tel obj;
obj.setdata();
obj.display();
ofstream fout;
fout.open("om.txt",ios::out);
fout.write((char *)&obj, sizeof(obj));
cout<<"successfully written on file: ";
fout.close();
ifstream fin;
tel newObj;
fin.open("om.txt",ios::in);
fin.read((char *)&newObj, sizeof(newObj));
cout<<" Read Succesfull"<<endl;
newObj.display();
getch();
}
#include <iostream.h>
#include <fstream.h>
/*
* Copy one file onto the end of another, adding line numbers
*/
int main () {
char myline[256];
int lc = 0;
ofstream outfile("demo.txt",ios::app);
ifstream infile("stdcodes.xyz");
if (! infile) {
cerr << "Failed to open input filen";
exit(1);
}
while (1) {
infile.getline(myline,256);
if (infile.eof()) break;
lc++;
outfile << lc << ": " << myline << "n";
}
infile.close();
outfile.close();
cout << "Output " << lc << " records" << endl;
}
/* Sample Output
munchkin:c235 grahamellis$ ./file01
Output 110 records
munchkin:c235 grahamellis$
*/
C++ program to write number 1 to 100 in a data file NOTES.TXT
#include<fstream.h>
int main()
{
ofstream fout;
fout.open("NOTES.TXT");
for(int i=1;i<=100;i++)
fout<<i<<endl;
fout.close();
return 0;
}
C++ program, which initializes a string variable and outputs the string to the disk file
#include<fstream.h>
int main()
{
ofstream fout;
fout.open("out.txt");
char str[300]="Time is a great teacher but unfortunately it kills
all its pupils. Berlioz";
fout<<str;
fout.close();
return 0;
}
User-defined function in C++ to read the content from a text file OUT.TXT, count and
display the number of alphabets present in it
void alphabets()
{
ifstream fin;
fin.open("out.txt");
char ch;
int count=0;
while(!fin.eof())
{
fin.get(ch);
if(isalpha(ch))
count++;
}
cout<<"Number of alphabets in file are "<<count;
fin.close();
}
User defined function in C++ to count the number of blank present in a text file named
"OUT.TXT".
void blankspace()
{
ifstream fin;
fin.open("out.txt");
char ch;
int count=0;
while(!fin.eof())
{
fin.get(ch);
if(ch==' ')
count++;
}
cout<<"Number of blank spaces in file are "<<count;
fin.close();
}
User defined function in C++ to print the count of word the as an independent word in a
text file STORY.TXT
void countword()
{
ifstream fin;
fin.open("STORY.TXT");
char word[30];
int count=0;
while(!fin.eof())
{
fin>>word;
if(strcmpi(word,"the")==0)
count++;
}
cout<<"Number of the word in file are "<<count;
fin.close();
}
Function in C++ to count and display the number of lines not starting with alphabet 'A'
present in a text file "STORY.TXT"
void countlines()
{
ifstream fin;
fin.open("STORY.TXT");
char str[80];
int count=0;
while(!fin.eof())
{
fin.getline(str,80);
if(str[0]!='A')
count++;
}
cout<<"Number of lines not starting with A are "<<count;
fin.close();
}
User defined function in C++ named copyupper(), that reads the file FIRST.TXT and
creates a new file named SECOND.TXT contains all words from the file FIRST.TXT in
uppercase
void copyupper()
{
ifstream fin;
fin.open("FIRST.TXT");
ofstream fout;
fout.open("SECOND.TXT");
char ch;
while(!fin.eof())
{
fin.get(ch);
ch=toupper(ch);
fout<<ch;
}
fin.close();
fout.close();
}
A C++ function, that reads the file FIRST.TXT and creates a new file named
SECOND.TXT, to contain only those words from the file FIRST.TXT which start with a
lowercase vowel
void vowelwords()
{
ifstream fin;
fin.open("FIRST.TXT");
ofstream fout;
fout.open("SECOND.TXT");
char word[30];
while(!fin.eof())
{
fin>>word;
if(word[0]=='a'||word[0]=='e'||word[0]=='i'||word[0]=='o'||word[0]=='u
')
fout<<word<<" ";
}
fin.close();
fout.close();
}
ser defined function in C++ to count number of words in a text file named "OUT.TXT"
void countwords()
{
ifstream fin;
fin.open("out.txt");
char word[30];
int count=0;
while(!fin.eof())
{
fin>>word;
count++;
}
cout<<"Number of words in file are "<<count;
fin.close();
}

More Related Content

What's hot

Mouse programming in c
Mouse programming in cMouse programming in c
Mouse programming in cgkgaur1987
 
Python 如何執行
Python 如何執行Python 如何執行
Python 如何執行kao kuo-tung
 
#OOP_D_ITS - 2nd - C++ Getting Started
#OOP_D_ITS - 2nd - C++ Getting Started#OOP_D_ITS - 2nd - C++ Getting Started
#OOP_D_ITS - 2nd - C++ Getting StartedHadziq Fabroyir
 
Applications of list
Applications of listApplications of list
Applications of listElavarasi K
 
Doublylinklist
DoublylinklistDoublylinklist
Doublylinklistritu1806
 
3 1. preprocessor, math, stdlib
3 1. preprocessor, math, stdlib3 1. preprocessor, math, stdlib
3 1. preprocessor, math, stdlib웅식 전
 
Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocationNaveen Gupta
 
Computer Practical
Computer PracticalComputer Practical
Computer PracticalPLKFM
 
C++ Programming - 1st Study
C++ Programming - 1st StudyC++ Programming - 1st Study
C++ Programming - 1st StudyChris Ohk
 
Intoduction to dynamic memory allocation
Intoduction to dynamic memory allocationIntoduction to dynamic memory allocation
Intoduction to dynamic memory allocationUtsav276
 

What's hot (20)

Queue oop
Queue   oopQueue   oop
Queue oop
 
Mouse programming in c
Mouse programming in cMouse programming in c
Mouse programming in c
 
Maintainable go
Maintainable goMaintainable go
Maintainable go
 
Python 如何執行
Python 如何執行Python 如何執行
Python 如何執行
 
C++ ARRAY WITH EXAMPLES
C++ ARRAY WITH EXAMPLESC++ ARRAY WITH EXAMPLES
C++ ARRAY WITH EXAMPLES
 
working with files
working with filesworking with files
working with files
 
#OOP_D_ITS - 2nd - C++ Getting Started
#OOP_D_ITS - 2nd - C++ Getting Started#OOP_D_ITS - 2nd - C++ Getting Started
#OOP_D_ITS - 2nd - C++ Getting Started
 
L8 file
L8 fileL8 file
L8 file
 
Headerfiles
HeaderfilesHeaderfiles
Headerfiles
 
Applications of list
Applications of listApplications of list
Applications of list
 
Doublylinklist
DoublylinklistDoublylinklist
Doublylinklist
 
See through C
See through CSee through C
See through C
 
3 1. preprocessor, math, stdlib
3 1. preprocessor, math, stdlib3 1. preprocessor, math, stdlib
3 1. preprocessor, math, stdlib
 
STL
STLSTL
STL
 
Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocation
 
Iostream in c++
Iostream in c++Iostream in c++
Iostream in c++
 
Computer Practical
Computer PracticalComputer Practical
Computer Practical
 
Let's golang
Let's golangLet's golang
Let's golang
 
C++ Programming - 1st Study
C++ Programming - 1st StudyC++ Programming - 1st Study
C++ Programming - 1st Study
 
Intoduction to dynamic memory allocation
Intoduction to dynamic memory allocationIntoduction to dynamic memory allocation
Intoduction to dynamic memory allocation
 

Viewers also liked

Viewers also liked (14)

Cprogramcontrolifelseselection3
Cprogramcontrolifelseselection3Cprogramcontrolifelseselection3
Cprogramcontrolifelseselection3
 
Software engineering marsic
Software engineering   marsicSoftware engineering   marsic
Software engineering marsic
 
using LPP
using LPPusing LPP
using LPP
 
Versioning
VersioningVersioning
Versioning
 
functional requirements using LPP
functional requirements using LPPfunctional requirements using LPP
functional requirements using LPP
 
Difference between c and c
Difference between c and cDifference between c and c
Difference between c and c
 
Sermon 10 10_10pm
Sermon 10 10_10pmSermon 10 10_10pm
Sermon 10 10_10pm
 
Sermon 10 10_10pm
Sermon 10 10_10pmSermon 10 10_10pm
Sermon 10 10_10pm
 
Oct conference1
Oct conference1Oct conference1
Oct conference1
 
Mighty warrior class 8 25-10
Mighty warrior class 8 25-10Mighty warrior class 8 25-10
Mighty warrior class 8 25-10
 
Mighty warrior class 8 25-10
Mighty warrior class 8 25-10Mighty warrior class 8 25-10
Mighty warrior class 8 25-10
 
Photoshop reference
Photoshop referencePhotoshop reference
Photoshop reference
 
Design concepts and principles
Design concepts and principlesDesign concepts and principles
Design concepts and principles
 
Design Principles
Design PrinciplesDesign Principles
Design Principles
 

Similar to Files

Write a C++ program 1. Study the function process_text() in file.pdf
Write a C++ program 1. Study the function process_text() in file.pdfWrite a C++ program 1. Study the function process_text() in file.pdf
Write a C++ program 1. Study the function process_text() in file.pdfjillisacebi75827
 
streams and files
 streams and files streams and files
streams and filesMariam Butt
 
source code which create file and write into it
source code which create file and write into itsource code which create file and write into it
source code which create file and write into itmelakusisay507
 
Lab01Filesbuild.bat@echo offclsset DRIVE_LETTER=1.docx
Lab01Filesbuild.bat@echo offclsset DRIVE_LETTER=1.docxLab01Filesbuild.bat@echo offclsset DRIVE_LETTER=1.docx
Lab01Filesbuild.bat@echo offclsset DRIVE_LETTER=1.docxDIPESH30
 
#include iostream #include cstring #include vector #i.pdf
 #include iostream #include cstring #include vector #i.pdf #include iostream #include cstring #include vector #i.pdf
#include iostream #include cstring #include vector #i.pdfanandatalapatra
 
lab03build.bat@echo offclsset DRIVE_LETTER=1set.docx
lab03build.bat@echo offclsset DRIVE_LETTER=1set.docxlab03build.bat@echo offclsset DRIVE_LETTER=1set.docx
lab03build.bat@echo offclsset DRIVE_LETTER=1set.docxDIPESH30
 
file handling final3333.pptx
file handling final3333.pptxfile handling final3333.pptx
file handling final3333.pptxradhushri
 
Presention programming
Presention programmingPresention programming
Presention programmingsaleha iqbal
 
9 character string &amp; string library
9  character string &amp; string library9  character string &amp; string library
9 character string &amp; string libraryMomenMostafa
 
Exmaples of file handling
Exmaples of file handlingExmaples of file handling
Exmaples of file handlingsparkishpearl
 
Chapter 13.1.10
Chapter 13.1.10Chapter 13.1.10
Chapter 13.1.10patcha535
 
Lecture 15_Strings and Dynamic Memory Allocation.pptx
Lecture 15_Strings and  Dynamic Memory Allocation.pptxLecture 15_Strings and  Dynamic Memory Allocation.pptx
Lecture 15_Strings and Dynamic Memory Allocation.pptxJawadTanvir
 

Similar to Files (20)

File handling in c++
File handling in c++File handling in c++
File handling in c++
 
Write a C++ program 1. Study the function process_text() in file.pdf
Write a C++ program 1. Study the function process_text() in file.pdfWrite a C++ program 1. Study the function process_text() in file.pdf
Write a C++ program 1. Study the function process_text() in file.pdf
 
streams and files
 streams and files streams and files
streams and files
 
source code which create file and write into it
source code which create file and write into itsource code which create file and write into it
source code which create file and write into it
 
String Manipulation Function and Header File Functions
String Manipulation Function and Header File FunctionsString Manipulation Function and Header File Functions
String Manipulation Function and Header File Functions
 
Data structures
Data structuresData structures
Data structures
 
Lab01Filesbuild.bat@echo offclsset DRIVE_LETTER=1.docx
Lab01Filesbuild.bat@echo offclsset DRIVE_LETTER=1.docxLab01Filesbuild.bat@echo offclsset DRIVE_LETTER=1.docx
Lab01Filesbuild.bat@echo offclsset DRIVE_LETTER=1.docx
 
#include iostream #include cstring #include vector #i.pdf
 #include iostream #include cstring #include vector #i.pdf #include iostream #include cstring #include vector #i.pdf
#include iostream #include cstring #include vector #i.pdf
 
lab03build.bat@echo offclsset DRIVE_LETTER=1set.docx
lab03build.bat@echo offclsset DRIVE_LETTER=1set.docxlab03build.bat@echo offclsset DRIVE_LETTER=1set.docx
lab03build.bat@echo offclsset DRIVE_LETTER=1set.docx
 
file handling final3333.pptx
file handling final3333.pptxfile handling final3333.pptx
file handling final3333.pptx
 
Presention programming
Presention programmingPresention programming
Presention programming
 
9 character string &amp; string library
9  character string &amp; string library9  character string &amp; string library
9 character string &amp; string library
 
7512635.ppt
7512635.ppt7512635.ppt
7512635.ppt
 
Exmaples of file handling
Exmaples of file handlingExmaples of file handling
Exmaples of file handling
 
C Programming Project
C Programming ProjectC Programming Project
C Programming Project
 
Python crush course
Python crush coursePython crush course
Python crush course
 
File Organization & processing Mid term summer 2014 - modelanswer
File Organization & processing Mid term summer 2014 - modelanswerFile Organization & processing Mid term summer 2014 - modelanswer
File Organization & processing Mid term summer 2014 - modelanswer
 
Chapter 13.1.10
Chapter 13.1.10Chapter 13.1.10
Chapter 13.1.10
 
Lecture 15_Strings and Dynamic Memory Allocation.pptx
Lecture 15_Strings and  Dynamic Memory Allocation.pptxLecture 15_Strings and  Dynamic Memory Allocation.pptx
Lecture 15_Strings and Dynamic Memory Allocation.pptx
 
Vcs28
Vcs28Vcs28
Vcs28
 

Files

  • 1. #include<iostream.h> #include<fstream.h> #include<math.h> void main() { clrscr(); ofstream fout("Myfile"); fout<<"Ganesh"; fout.close(); ifstream fin("Myfile"); char ch; while(fin) { fin.get(ch); cout<<ch; } fin.close(); getch(); } 52. Write a program that will create a data file containing the list of telephone numbers and name. Use a class object to store each set of data. /* * To write the complex data types like array, structure or * classes, we have already discussed that only one method * for read and one method for write is used : * These are - fout.write((char *)&obj, sizeof(obj)); * and fin.read((char *)&newObj, sizeof(newObj)); */ #include<fstream.h> #include<conio.h>
  • 2. #include<iostream.h> #include<fstream.h> class tel { private: char *name, *number; public: void setdata() { cout<<"Enter Name and number"<<endl; cin>>name>>number; } void display() { cout<<"Name : "<<name<<endl; cout<<"Number : "<<number<<endl; } }; void main() { tel obj;
  • 3. obj.setdata(); obj.display(); ofstream fout; fout.open("om.txt",ios::out); fout.write((char *)&obj, sizeof(obj)); cout<<"successfully written on file: "; fout.close(); ifstream fin; tel newObj; fin.open("om.txt",ios::in); fin.read((char *)&newObj, sizeof(newObj)); cout<<" Read Succesfull"<<endl; newObj.display(); getch(); } #include <iostream.h> #include <fstream.h> /* * Copy one file onto the end of another, adding line numbers */ int main () { char myline[256]; int lc = 0;
  • 4. ofstream outfile("demo.txt",ios::app); ifstream infile("stdcodes.xyz"); if (! infile) { cerr << "Failed to open input filen"; exit(1); } while (1) { infile.getline(myline,256); if (infile.eof()) break; lc++; outfile << lc << ": " << myline << "n"; } infile.close(); outfile.close(); cout << "Output " << lc << " records" << endl; } /* Sample Output munchkin:c235 grahamellis$ ./file01 Output 110 records munchkin:c235 grahamellis$ */ C++ program to write number 1 to 100 in a data file NOTES.TXT #include<fstream.h> int main() { ofstream fout; fout.open("NOTES.TXT"); for(int i=1;i<=100;i++) fout<<i<<endl; fout.close(); return 0; }
  • 5. C++ program, which initializes a string variable and outputs the string to the disk file #include<fstream.h> int main() { ofstream fout; fout.open("out.txt"); char str[300]="Time is a great teacher but unfortunately it kills all its pupils. Berlioz"; fout<<str; fout.close(); return 0; } User-defined function in C++ to read the content from a text file OUT.TXT, count and display the number of alphabets present in it void alphabets() { ifstream fin; fin.open("out.txt"); char ch; int count=0; while(!fin.eof()) { fin.get(ch); if(isalpha(ch)) count++; } cout<<"Number of alphabets in file are "<<count; fin.close(); } User defined function in C++ to count the number of blank present in a text file named "OUT.TXT". void blankspace() { ifstream fin;
  • 6. fin.open("out.txt"); char ch; int count=0; while(!fin.eof()) { fin.get(ch); if(ch==' ') count++; } cout<<"Number of blank spaces in file are "<<count; fin.close(); } User defined function in C++ to print the count of word the as an independent word in a text file STORY.TXT void countword() { ifstream fin; fin.open("STORY.TXT"); char word[30]; int count=0; while(!fin.eof()) { fin>>word; if(strcmpi(word,"the")==0) count++; } cout<<"Number of the word in file are "<<count; fin.close(); } Function in C++ to count and display the number of lines not starting with alphabet 'A' present in a text file "STORY.TXT" void countlines() { ifstream fin; fin.open("STORY.TXT"); char str[80]; int count=0; while(!fin.eof()) { fin.getline(str,80); if(str[0]!='A')
  • 7. count++; } cout<<"Number of lines not starting with A are "<<count; fin.close(); } User defined function in C++ named copyupper(), that reads the file FIRST.TXT and creates a new file named SECOND.TXT contains all words from the file FIRST.TXT in uppercase void copyupper() { ifstream fin; fin.open("FIRST.TXT"); ofstream fout; fout.open("SECOND.TXT"); char ch; while(!fin.eof()) { fin.get(ch); ch=toupper(ch); fout<<ch; } fin.close(); fout.close(); } A C++ function, that reads the file FIRST.TXT and creates a new file named SECOND.TXT, to contain only those words from the file FIRST.TXT which start with a lowercase vowel void vowelwords() { ifstream fin; fin.open("FIRST.TXT"); ofstream fout; fout.open("SECOND.TXT"); char word[30]; while(!fin.eof()) { fin>>word; if(word[0]=='a'||word[0]=='e'||word[0]=='i'||word[0]=='o'||word[0]=='u ')
  • 8. fout<<word<<" "; } fin.close(); fout.close(); } ser defined function in C++ to count number of words in a text file named "OUT.TXT" void countwords() { ifstream fin; fin.open("out.txt"); char word[30]; int count=0; while(!fin.eof()) { fin>>word; count++; } cout<<"Number of words in file are "<<count; fin.close(); }