1 / 27

Lecture# 26 Programming Concepts

Lecture# 26 Programming Concepts. File Handling. Steps to handle file. Open the File Read / Write the File Close the File. Header File for File Handling. fstream.h #include <fstream.h> Input File Stream ifstream Output file stream ofstream. Reading File. #include <fstream.h>

tassos
Télécharger la présentation

Lecture# 26 Programming Concepts

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture# 26 Programming Concepts

  2. File Handling

  3. Steps to handle file • Open the File • Read / Write the File • Close the File

  4. Header File for File Handling • fstream.h • #include <fstream.h> • Input File Stream • ifstream • Output file stream • ofstream

  5. Reading File • #include <fstream.h> • ifstream myFile ; • myFile.open ( “File.txt” ) ; c:\myProg\File.txt • myFile.close();

  6. My.txt myfile My.txt myfile Process: close myfile.open ( “My.txt” ,ios::in) ; Process: open myfile.close ( “My.txt” ) ;

  7. ifstream myFile; • myFile.open("File.txt“,ios::in); • if(!myFile){ • cout<<"The file cannot open"; • return 0; • } • char a; • myFile>>a; • cout<<a; • myFile.close();

  8. void main(){ int x; cin>>x; cout<<x; ifstream hFile; hFile.open(“File.txt”,ios::in); char a; hFile>>a; cout<<a; getch() } >> << 9 T >> This is a File.txt

  9. ifstream myFile; • myFile.open("File.txt"); • if(!myFile){ • cout<<"The file cannot open"; • return 0; • } • char a[10]; • myFile>>a; • cout<<a; • myFile.close();

  10. myfile.eof ( ) • while ( !myfile.eof ( ) ) • { • myfile >> varName ; • cout<<varName; • }

  11. get() • while ( !myFile.eof ( ) ) • { • myFile.get ( ch ) ; • cout << ch ; • }

  12. Generic Syntax • fstream Myfile • Myfile.open( “File.txt” , mode ) ; // Generic syntax ifstream • Myfile.open ( “File.txt” , ios :: in ) ; OR • Myfile.open ( “File.txt” , ios :: out ) ;

  13. Read \ Write • ifstream myInputFile ( “myfile.txt” , ios :: in ) ; • ofstream myOutputFile ( “myfile.txt” , ios :: out ) ;

  14. Output File Modes • Create a new file • Overwrite an existing file • Append some text • Randomly accessing a file

  15. #include<fstream> • void main(){ • ofstream myFile("File.txt",ios::out); • if ( !myFile ) // Error check • { • cout << "Your file could not be opened"; • } • int i=0; • char a[100];

  16. gets(a); • while(a[i]!='\0'){ • myFile<<a[i]; • i++; • } • myFile.close ( ) ; • getch(); • }

  17. Example

  18. #include<fstream> • #include<stdlib> • int main(){ • ifstream hFile("File.txt",ios::in); • if(!hFile){ • cerr<<"File cannot open"; • getch(); • exit(1); • }

  19. int account; • char name[30]; • float balance; • cout<<"Account\t"<<"Name\t"<<"Balance\n"; • while(!hFile.eof()){ • hFile>>account>>name>>balance; • cout<<account<<"\t"<<name<<"\t"<<balance<<endl; • } • getch(); • return 0; • }

  20. while( hFile>>account>>name>>balance){ • cout<<account<<"\t"<<name<<"\t"<<balance<<endl; • }

  21. File opening Modes • ios :: in open for reading (default for ifstream) • ios :: out • open for writing (default for ofstream) • ios :: app start writing at end of file (APPend) • ios :: ate start reading or writing at EOF of file (ATEnd) • ios :: trunc truncate file to zero length if it exists (TRUNCate)

  22. File opening Modes • ios :: nocreate error when opening if file does not already exist • ios :: noreplace error when opening for output if file already exists • ios :: binary open file in binary (not text) mode

  23. Problems • Copy the contents of one text file on the other text file. • Copy the contents of one text file at the end of other text file

  24. Opening any File • #include<iostream.h> • #include<conio.h> • #include<fstream.h> • #include<stdio.h> • int main(){ • clrscr(); • fstream myFile; • char name[10]; • cout<<"Enter the name of file want to Open:"; • gets(name); • myFile.open(name,ios::in);

  25. if(!myFile){ • cout<<"The file cannot open:"; • getch(); • return 0; • } • char a; • while(!myFile.eof()){ • myFile.get(a); • cout<<a; • } • myFile.close(); • getch(); • return 0; • }

  26. #include<iostream.h> • #include<conio.h> • #include<fstream.h> • #include<stdio.h> • int main(){ • clrscr(); • fstream myFile1,myFile; • char name[10],name1[10]; • cout<<"Enter the name of file want to copy:"; • gets(name); • myFile.open(name,ios::in); • cout<<"\nEnter the name of file where u want to paste:"; • gets(name1); • myFile1.open(name1,ios::out);

  27. if(!myFile){ • cout<<"The file cannot open:"; • getch(); • return 0; • } • char a; • while(!myFile.eof()){ • myFile.get(a); • myFile1.put(a); • } • myFile.close(); • myFile1.close(); • getch(); • return 0; • }

More Related