Files

567 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
567
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
11
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Files

  1. 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 numbersand 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. 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. 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. 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 Outputmunchkin:c235 grahamellis$ ./file01Output 110 recordsmunchkin: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. 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 killsall 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 anddisplay the number of alphabets present in itvoid 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. 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 atext file STORY.TXTvoid 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 Apresent 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. 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 andcreates a new file named SECOND.TXT contains all words from the file FIRST.TXT inuppercasevoid 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 namedSECOND.TXT, to contain only those words from the file FIRST.TXT which start with alowercase vowelvoid 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. 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();}

×