Transcript Lecture# 26

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>
• ifstream myFile ;
• myFile.open ( “File.txt” ) ;
c:\myProg\File.txt
• myFile.close();
myfile.open ( “My.txt” ,ios::in) ;
Process: open
myfile
My.txt
myfile.close ( “My.txt” ) ;
Process: close
myfile
My.txt
•
•
•
•
•
•
•
•
•
•
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();
void main(){
int x;
cin>>x;
cout<<x;
ifstream hFile;
>>
<<
9
hFile.open(“File.txt”,ios::in);
char a;
hFile>>a;
cout<<a;
getch()
}
>>
This is a
File.txt
T
•
•
•
•
•
•
•
•
•
•
ifstream myFile;
myFile.open("File.txt");
if(!myFile){
cout<<"The file cannot open";
return 0;
}
char a[10];
myFile>>a;
cout<<a;
myFile.close();
myfile.eof ( )
• while ( !myfile.eof ( ) )
•
{
• myfile >> varName ;
• cout<<varName;
•
}
get()
•
•
•
•
•
while ( !myFile.eof ( ) )
{
myFile.get ( ch ) ;
cout << ch ;
}
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 ) ;
Read \ Write
ifstream myInputFile ( “myfile.txt” , ios :: in ) ;
ofstream myOutputFile ( “myfile.txt” , ios :: out )
;
Output File Modes
•
•
•
•
Create a new file
Overwrite an existing file
Append some text
Randomly accessing a file
• #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];
• gets(a);
• while(a[i]!='\0'){
• myFile<<a[i];
• i++;
•}
• myFile.close ( ) ;
• getch();
•}
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<<"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"<<balanc
e<<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;