Files

9

Click here to load reader

Transcript of Files

Page 1: Files

#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>

Page 2: Files

#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;

Page 3: Files

       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;

Page 4: Files

ofstream outfile("demo.txt",ios::app);

ifstream infile("stdcodes.xyz"); if (! infile) { cerr << "Failed to open input file\n"; 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$ ./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;

}

Page 5: Files

 

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;

Page 6: Files

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')

Page 7: Files

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<<" ";

Page 8: Files

}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();

}