370 likes | 454 Views
Lecture# 27 Programming Concepts. Example. #include<fstream> #include<stdlib> int main(){ ifstream hFile("File.txt",ios::in); if(!hFile){ cerr<<"File cannot open"; getch(); exit(1); }. int account; char name[30]; float balance; cout<<"Accountt"<<"Namet"<<"Balance<br>";
E N D
Lecture# 27 Programming Concepts
#include<fstream> • #include<stdlib> • int main(){ • ifstream hFile("File.txt",ios::in); • if(!hFile){ • cerr<<"File cannot open"; • getch(); • exit(1); • }
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; • }
while( hFile>>account>>name>>balance){ • cout<<account<<"\t"<<name<<"\t"<<balance<<endl; • }
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)
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
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
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);
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; • }
#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);
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; • }
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 ( ) ;
getline ( ) function myfile.getline (char *s, int n, char delim); Number of Characters to Be read Character Array delimiter
Syntax of getline function • Input • Hello World • myfile.getline ( arrayString , 20 , ’W’ ) ; • Output • Hello
Output File Stream • ios :: app • ios :: out • ios :: ate
Read/write a character • stream extraction operator (>>) • stream insertion operator (<<) • get ( ) Read a character • put ( ) Write a character • getline(charArray,20,’\n’);
Random File • Position • File Position Pointer
tellg ( ) Function • myFile.tellg ( ) ; Returns a whole number which tell you the position of the next character to be read from the file
tellp ( ) Function • myFile.tellp ( ) ; Returns a whole number which tell you the position of the next character to be written in a file
For Positioning in the file • seekg ( ) ; • seekp ( ) ;
seekg ( ) • filePtr.seekg ( long Num , ios :: origin ) ; Number of characters to move from Starting point Number of characters to move to Starting point
seekg ( ) • seekg ( 10L , ios :: beg ) ; • seekg (10L , ios :: cur ) ; • seekg ( 10L , ios :: end ) ;
Example 1 • #include<fstream.h> • main ( ) { • int length ; • ifstream inFile ( “myFile.txt” ) ; • inFile.seekg ( 0L , ios :: end ) ; • length = inFile.tellg ( ) ; • }
fstream • fstream myFile ( “Sample.txt” , ios :: in | ios :: out ) ;
Example 2 This is an Apple This is a Sample
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; • }
Macro • #define PI 3.141592 • Parameterized Macro
Example • #define SQUARE ( X ) X * X • main ( ) { • int i = 5 , j ; • j =SQUARE ( i ) ; • }
Example 2 • #define SQUARE ( X ) X * X • main ( ) { • Int i = 5 , j = 10 , k ; • k =SQUARE ( i + j ) ; • } • // i+j*i+j
Example 3 • #define SQUARE(X) (X)*(X) • main ( ) { • int i = 5 , j = 10 , k ; • k = SQUARE ( i + j ) ; • }
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); • }
String Handling • Character • ASCII • Representation in 1 byte = 8 bits • Header File • ctype.h • #include<ctype.h>
String Handling • #include<iostream.h> • main ( ) { • int i ; • char c ; • for( i = 0; i < 256 ; i ++ ) { • c = i ; cout << i << “\t” << c <<endl ; } • }
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 ( ) ;
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 ++ ; • }