data file handling - lpu guide€¦ · data file handling. introduction •computer programs are...
TRANSCRIPT
![Page 1: Data File Handling - LPU GUIDE€¦ · Data File Handling. Introduction •Computer programs are associated to work with files as it helps in storing data & information permanently](https://reader035.vdocuments.mx/reader035/viewer/2022062506/5f0675e87e708231d4181b6d/html5/thumbnails/1.jpg)
Data File Handling
![Page 2: Data File Handling - LPU GUIDE€¦ · Data File Handling. Introduction •Computer programs are associated to work with files as it helps in storing data & information permanently](https://reader035.vdocuments.mx/reader035/viewer/2022062506/5f0675e87e708231d4181b6d/html5/thumbnails/2.jpg)
Introduction
• Computer programs are associated to workwith files as it helps in storing data &information permanently.
• File - itself a bunch of bytes stored on somestorage devices.
• In C++ this is achieved through a componentheader file called fstream
![Page 3: Data File Handling - LPU GUIDE€¦ · Data File Handling. Introduction •Computer programs are associated to work with files as it helps in storing data & information permanently](https://reader035.vdocuments.mx/reader035/viewer/2022062506/5f0675e87e708231d4181b6d/html5/thumbnails/3.jpg)
Contd…
• Earlier, only two streams are being used such as cin
and cout.
• There are some problems with these streams.
1) It is very difficult to handle large volume of data.
2) The data is not permanently stored.
Eg:- the calculation of the result of 200 students will
be lost when the program terminates.
![Page 4: Data File Handling - LPU GUIDE€¦ · Data File Handling. Introduction •Computer programs are associated to work with files as it helps in storing data & information permanently](https://reader035.vdocuments.mx/reader035/viewer/2022062506/5f0675e87e708231d4181b6d/html5/thumbnails/4.jpg)
Contd…..
• C++ provides a new technique for handling I/O
operations through mechanism known as streams.
• A stream refers to a flow of data.
• Classified in 2 categories:
1. Output stream
2. Input stream
In output stream flow of data is from program to the
output device.
In input stream the flow of data is from input device to
a program in main memory.
![Page 5: Data File Handling - LPU GUIDE€¦ · Data File Handling. Introduction •Computer programs are associated to work with files as it helps in storing data & information permanently](https://reader035.vdocuments.mx/reader035/viewer/2022062506/5f0675e87e708231d4181b6d/html5/thumbnails/5.jpg)
File Program ( Input stream) - readsProgram File (Output stream) – write
![Page 6: Data File Handling - LPU GUIDE€¦ · Data File Handling. Introduction •Computer programs are associated to work with files as it helps in storing data & information permanently](https://reader035.vdocuments.mx/reader035/viewer/2022062506/5f0675e87e708231d4181b6d/html5/thumbnails/6.jpg)
Stream Class Hierarchy
![Page 7: Data File Handling - LPU GUIDE€¦ · Data File Handling. Introduction •Computer programs are associated to work with files as it helps in storing data & information permanently](https://reader035.vdocuments.mx/reader035/viewer/2022062506/5f0675e87e708231d4181b6d/html5/thumbnails/7.jpg)
Why to use Files:
• Convenient way to deal large quantities of data.
• Store data permanently (until file is deleted).
• Avoid typing data into program multiple times.
• Share data between programs.
We need to know:
how to "connect" file to program
how to tell the program to read data
how to tell the program to write data
error checking and handling EOF
![Page 8: Data File Handling - LPU GUIDE€¦ · Data File Handling. Introduction •Computer programs are associated to work with files as it helps in storing data & information permanently](https://reader035.vdocuments.mx/reader035/viewer/2022062506/5f0675e87e708231d4181b6d/html5/thumbnails/8.jpg)
• A file can be opened in two ways:
1. Using constructor function
2. Using member function
![Page 9: Data File Handling - LPU GUIDE€¦ · Data File Handling. Introduction •Computer programs are associated to work with files as it helps in storing data & information permanently](https://reader035.vdocuments.mx/reader035/viewer/2022062506/5f0675e87e708231d4181b6d/html5/thumbnails/9.jpg)
Opening file using constructor
• File name is used to initialize the file stream object.
• Steps:-
1. Create a file stream object to manage stream with
appropriate class.
2. Initialize the file object with desired name.
![Page 10: Data File Handling - LPU GUIDE€¦ · Data File Handling. Introduction •Computer programs are associated to work with files as it helps in storing data & information permanently](https://reader035.vdocuments.mx/reader035/viewer/2022062506/5f0675e87e708231d4181b6d/html5/thumbnails/10.jpg)
Contd……..
• ofstream outfile(“results.txt”); //it opens a file name “results.txt” for output.
• ifstream infile(“data.txt”);
outfile<<“total”;
outfile.close();
infile>> string;
![Page 11: Data File Handling - LPU GUIDE€¦ · Data File Handling. Introduction •Computer programs are associated to work with files as it helps in storing data & information permanently](https://reader035.vdocuments.mx/reader035/viewer/2022062506/5f0675e87e708231d4181b6d/html5/thumbnails/11.jpg)
Opening files using member function open()
• This function is used to open multiple files that uses
same stream object.
• Syntax:-
file_stream class stream_object;
stream_object.open(“filename”);
![Page 12: Data File Handling - LPU GUIDE€¦ · Data File Handling. Introduction •Computer programs are associated to work with files as it helps in storing data & information permanently](https://reader035.vdocuments.mx/reader035/viewer/2022062506/5f0675e87e708231d4181b6d/html5/thumbnails/12.jpg)
Example
ofstream outfile;
outfile.open(“Data”);
…………………..
…………………..
outfile.close();
outfile.open(“Data2”);
…………………….
…………………..
outfile.close();
![Page 13: Data File Handling - LPU GUIDE€¦ · Data File Handling. Introduction •Computer programs are associated to work with files as it helps in storing data & information permanently](https://reader035.vdocuments.mx/reader035/viewer/2022062506/5f0675e87e708231d4181b6d/html5/thumbnails/13.jpg)
Open(): File Modes
• We can have two arguments in open(), to specify the
file-mode.
stream-object.open(“filename”,mode);
the second argument specifies the purpose for which
file is opened.
![Page 14: Data File Handling - LPU GUIDE€¦ · Data File Handling. Introduction •Computer programs are associated to work with files as it helps in storing data & information permanently](https://reader035.vdocuments.mx/reader035/viewer/2022062506/5f0675e87e708231d4181b6d/html5/thumbnails/14.jpg)
MEMBER FUNCTIONS
• ios::app = append at end of file
• ios::ate = go to end of file on opening instead of
beginning.
• ios::binary=binary file
• ios::in = open file for reading only
• ios::out = open file for writing only
• ios:: trunc = delete the content of file if it exists
• ios::nocreate = open fails if file doesn’t exist.
![Page 15: Data File Handling - LPU GUIDE€¦ · Data File Handling. Introduction •Computer programs are associated to work with files as it helps in storing data & information permanently](https://reader035.vdocuments.mx/reader035/viewer/2022062506/5f0675e87e708231d4181b6d/html5/thumbnails/15.jpg)
EXAMPLE
1) ofstream fileout;
fileout.open(“hello.txt”,ios::app);
2)fileout.open(“hello.txt”, ios::in|ios::out);
![Page 16: Data File Handling - LPU GUIDE€¦ · Data File Handling. Introduction •Computer programs are associated to work with files as it helps in storing data & information permanently](https://reader035.vdocuments.mx/reader035/viewer/2022062506/5f0675e87e708231d4181b6d/html5/thumbnails/16.jpg)
Stream state member functions
• In C++, file stream classes inherit a stream state
member from the ios class, which gives out the
information regarding the status of the stream.
For e.g.:
eof() –used to check the end of file character
fail()- used to check the status of file at opening for
I/O
bad()- used to check whether invalid file operations
or unrecoverable error .
good()- used to check whether the previous file
operation has been successful
![Page 17: Data File Handling - LPU GUIDE€¦ · Data File Handling. Introduction •Computer programs are associated to work with files as it helps in storing data & information permanently](https://reader035.vdocuments.mx/reader035/viewer/2022062506/5f0675e87e708231d4181b6d/html5/thumbnails/17.jpg)
EXAMPLES
• EOF()
void main()
{
ifstream infile;
infile.open(“text”);
while(!infile.eof())
{
------
-----
}
}
![Page 18: Data File Handling - LPU GUIDE€¦ · Data File Handling. Introduction •Computer programs are associated to work with files as it helps in storing data & information permanently](https://reader035.vdocuments.mx/reader035/viewer/2022062506/5f0675e87e708231d4181b6d/html5/thumbnails/18.jpg)
• FAIL()
main()
{
ifstream infile;
infile.open(“text”);
while(!infile.fail())
{
cout<<“cudn’t open a file”;
}
}
![Page 19: Data File Handling - LPU GUIDE€¦ · Data File Handling. Introduction •Computer programs are associated to work with files as it helps in storing data & information permanently](https://reader035.vdocuments.mx/reader035/viewer/2022062506/5f0675e87e708231d4181b6d/html5/thumbnails/19.jpg)
Reading and Writing in Files
• Reading and Writing a character from a file can
be done by
– Get()
– Put()
• Get():- This is used to read an alphanumeric
character from a specified file.
• Put():- This is used to write an alphanumeric
character to specified file.
![Page 20: Data File Handling - LPU GUIDE€¦ · Data File Handling. Introduction •Computer programs are associated to work with files as it helps in storing data & information permanently](https://reader035.vdocuments.mx/reader035/viewer/2022062506/5f0675e87e708231d4181b6d/html5/thumbnails/20.jpg)
Reading and writing by insertion and
extraction
Stream Insertion Operators
• Are defined in the ostream class
• The operator “<<” is called the inserter
Stream Extraction Operators
• Are defined in the istream class and are used to receive data
from the input device
• The operator “>>”, called the extractor.
![Page 21: Data File Handling - LPU GUIDE€¦ · Data File Handling. Introduction •Computer programs are associated to work with files as it helps in storing data & information permanently](https://reader035.vdocuments.mx/reader035/viewer/2022062506/5f0675e87e708231d4181b6d/html5/thumbnails/21.jpg)
By Read () and Write()
• Read and write is used when we are dealing with
classes.
• Syntax:-
Read((char*)&obj sizeof(obj));
Write((char*)&obj,sizeof(obj));
![Page 22: Data File Handling - LPU GUIDE€¦ · Data File Handling. Introduction •Computer programs are associated to work with files as it helps in storing data & information permanently](https://reader035.vdocuments.mx/reader035/viewer/2022062506/5f0675e87e708231d4181b6d/html5/thumbnails/22.jpg)
EXAMPLEclass emp
{
private:
int empno;
char name[20];
public:
void getdetails()
{
cout<<"enter name,emp no and salary";
cin>>name>>empno;
}
void display()
{
cout<<"\n";
cout<<empno<<name;
}
};
int main()
{
ofstream f1;
int n,i;
f1.open("emp.txt");
cout<<"enter the number of employees";
cin>>n;
emp e[n];
for(i=0;i<n;i++)
{
e[i].getdetails();
f1.write((char*) &e[i],sizeof(e[i]));
}
f1.close();
ifstream f2;
f2.open("emp.txt");
for(i=0;i<n;i++)
{
f2.read((char*)&e[i],sizeof(e[i]));
e[i].display();
}
f2.close();
getch();
Return 0;
}
![Page 23: Data File Handling - LPU GUIDE€¦ · Data File Handling. Introduction •Computer programs are associated to work with files as it helps in storing data & information permanently](https://reader035.vdocuments.mx/reader035/viewer/2022062506/5f0675e87e708231d4181b6d/html5/thumbnails/23.jpg)
Functions for manipulation of File Pointers
• seekg() : Moves get pointer (input)to a specified Location.
• seekp() : Moves put pointer (output)to a specified Location.
• tellg() : Gives current position of the get pointer.
• tellp() : Gives current position of the put pointer.
![Page 24: Data File Handling - LPU GUIDE€¦ · Data File Handling. Introduction •Computer programs are associated to work with files as it helps in storing data & information permanently](https://reader035.vdocuments.mx/reader035/viewer/2022062506/5f0675e87e708231d4181b6d/html5/thumbnails/24.jpg)
value offset is relative to...
ios::beg beginning of the stream
ios::cur current position in the stream
ios::end end of the stream
![Page 25: Data File Handling - LPU GUIDE€¦ · Data File Handling. Introduction •Computer programs are associated to work with files as it helps in storing data & information permanently](https://reader035.vdocuments.mx/reader035/viewer/2022062506/5f0675e87e708231d4181b6d/html5/thumbnails/25.jpg)