1 / 37

Lecture# 27 Programming Concepts

Lecture# 27 Programming Concepts. Example. #include&lt;fstream&gt; #include&lt;stdlib&gt; int main(){ ifstream hFile(&quot;File.txt&quot;,ios::in); if(!hFile){ cerr&lt;&lt;&quot;File cannot open&quot;; getch(); exit(1); }. int account; char name[30]; float balance; cout&lt;&lt;&quot;Accountt&quot;&lt;&lt;&quot;Namet&quot;&lt;&lt;&quot;Balance<br>&quot;;

Download Presentation

Lecture# 27 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# 27 Programming Concepts

  2. Example

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

  4. 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; • }

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

  6. 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)

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

  8. 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

  9. 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);

  10. 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; • }

  11. #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);

  12. 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; • }

  13. Steps to handle file • Open the File ifstream or ofstream or fstream myFile ; myFile.open ( “d:\c++\File.txt”,ios::out ) ; • Read / Write the File char var; myfile >> var1; • Close the File myFile.close ( ) ;

  14. getline ( ) function myfile.getline (char *s, int n, char delim); Number of Characters to Be read Character Array delimiter

  15. Syntax of getline function • Input • Hello World • myfile.getline ( arrayString , 20 , ’W’ ) ; • Output • Hello

  16. Output File Stream • ios :: app • ios :: out • ios :: ate

  17. Read/write a character • stream extraction operator (>>) • stream insertion operator (<<) • get ( ) Read a character • put ( ) Write a character • getline(charArray,20,’\n’);

  18. Random File • Position • File Position Pointer

  19. tellg ( ) Function • myFile.tellg ( ) ; Returns a whole number which tell you the position of the next character to be read from the file

  20. tellp ( ) Function • myFile.tellp ( ) ; Returns a whole number which tell you the position of the next character to be written in a file

  21. For Positioning in the file • seekg ( ) ; • seekp ( ) ;

  22. seekg ( ) • filePtr.seekg ( long Num , ios :: origin ) ; Number of characters to move from Starting point Number of characters to move to Starting point

  23. seekg ( ) • seekg ( 10L , ios :: beg ) ; • seekg (10L , ios :: cur ) ; • seekg ( 10L , ios :: end ) ;

  24. Example 1 • #include<fstream.h> • main ( ) { • int length ; • ifstream inFile ( “myFile.txt” ) ; • inFile.seekg ( 0L , ios :: end ) ; • length = inFile.tellg ( ) ; • }

  25. fstream • fstream myFile ( “Sample.txt” , ios :: in | ios :: out ) ;

  26. Example 2 This is an Apple This is a Sample

  27. int main(){ • ofstream hFile1("abc.txt",ios::ate); • if(!hFile1){ • cerr<<"File cannot open"; • getch(); • exit(1); • } • hFile1.seekp(9L,ios::beg); • char*p=" Sample"; • hFile1<<p; • getch(); • return 0; • }

  28. Macro • #define PI 3.141592 • Parameterized Macro

  29. Example • #define SQUARE ( X ) X * X • main ( ) { • int i = 5 , j ; • j =SQUARE ( i ) ; • }

  30. Example 2 • #define SQUARE ( X ) X * X • main ( ) { • Int i = 5 , j = 10 , k ; • k =SQUARE ( i + j ) ; • } • // i+j*i+j

  31. Example 3 • #define SQUARE(X) (X)*(X) • main ( ) { • int i = 5 , j = 10 , k ; • k = SQUARE ( i + j ) ; • }

  32. Inline function • inline int f ( int a, int b ) { • if ( a > b ) • return a ; • return b ; • } • void main ( ) { • int i , x , y ; • x = 23 ; • y = 45 ; • i=f(x,y); • }

  33. String Handling • Character • ASCII • Representation in 1 byte = 8 bits • Header File • ctype.h • #include<ctype.h>

  34. String Handling • #include<iostream.h> • main ( ) { • int i ; • char c ; • for( i = 0; i < 256 ; i ++ ) { • c = i ; cout << i << “\t” << c <<endl ; } • }

  35. String handling Functions • int isdigit ( char c ) ; • int isalpha ( char c ) ; • int islower ( char c ) ; • int isupper ( char c ) ; • int tolower ( int c ) ; • int toupper ( int c ) ; • getchar ( ) ;

  36. char c; • cout << “Please enter a character string then press enter”; • while ( ( c = getchar ( ) ) != ‘\n’ ) { • if ( islower ( c ) ) lc ++ ; • else if ( isupper ( c ) ) • uc ++ ; • else if (isdigit ( c ) ) • dig ++; • else if ( isspace ( c ) ) • ws ++ ; • else if ( ispunct ( c ) ) • pun ++ ; • else • oth ++ ; • }

More Related