Lec13 (Topic 8 File Handling)
Lec13 (Topic 8 File Handling)
File Handling
Overview
13.1 Concept of a File
Files and Streams
13.2 Standard File Handling Functions
13.3 Binary Files
13.4 Random Access Files
13.1
Concept of a File
Files and Streams
I/O Streams
• I/O refers to program input and output
• Input is delivered to your program via a stream object
• Input can be from
• The keyboard
• A file
• Output is delivered to the output device via a stream
object
• Output can be to
• The screen
• A file
Streams and Basic File I/O
• Files for I/O are the same type of files used to
store programs
• A stream is a flow of data.
• Input stream: Data flows into the program
• If input stream flows from keyboard, the program will
accept data from the keyboard
• If input stream flows from a file, the program will accept
data from the file
• Output stream: Data flows out of the program
• To the screen
• To a file
cin And cout Streams
• cin
• Input stream connected to the keyboard
• cout
• Output stream connected to the screen
• cin and cout defined in the iostream library
• Use include directive: #include <iostream>
•
in_stream.open("infile.dat");
Double quotes
Period
Dot operator
Errors On Opening Files
• Opening a file could fail for several reasons
• Common reasons for open to fail include
• The file might not exist
• The name might be typed incorrectly
in_stream.open("stuff.dat");
if( in_stream.fail( ) )
{
cout << "Input file opening failed.\n";
exit(1) ;
}
Techniques for File I/O
• When reading input from a file…
• Do not include prompts or echo the input
• The lines cout << "Enter the number: ";
cin >> the_number;
cout << "The number you entered is "
<< the_number;
become just one line
Slide 6- 32
End of File Example
• To calculate the average of the numbers in a file
• double next, sum = 0;
int count = 0;
while(in_stream >> next)
{
sum = sum + next;
count++;
}
Slide 6- 33
13.2
Standard File Handling Functions
Step 1
• Include the proper directory to use all the file stream
member methods.
#include <fstream>
Step 2
• Create a file instance
• For reading from files
• ifstream myfile;
• For writing to files
• ofstream myfile;
else
exit(1);
fstream myfile("tmp.txt", ios::in | ios::binary);
if(myfile){
cout << "Success\n";
//perform operations needed
}
else
exit(1);
Step 5
• Writing to a file
1) Using the insertion operator <<
myfile << "Hello world!\nHow are we
today?\n";
2) Using the put() method
char ch;
cout << "Type some text (type a dot to finish):\n";
while((ch = cin.get())
&& (ch!='.')
&& (myfile.put(ch)))
{}
Step 6
• Reading from a file
1) Using the extraction operator >>
string tmp;
while(!myfile.eof()){
myfile >> tmp;
if(myfile.good())
cout << tmp << " ";
}
endl(cout);
2) Using the get() method
char tmp;
while(myfile.good()){
tmp = myfile.get();
if(myfile.good())
cout << tmp;
}
3) Using the getline() method – string version
string tmp;
while(myfile.good()){
getline(myfile, tmp);
if(myfile.good())
cout << tmp << endl;
}
Step 7
• Closing a file
int main () {
fstream myfile("tmp.txt", ios::out);
myfile << "sample test string";
myfile.close();
myfile.open("tmp.txt", ios::in);
string tmp;
getline(myfile, tmp);
cout << tmp << endl;
myfile.close();
return 0;
}
13.3
Binary Files
Binary
• Data written into a binary files are not so easily readable.
• Access to a binary file deals with memory blocks.
• Therefore, it’s easier to manipulate especially when
having more complex data structures like classes and/or
arrays.
Writing to a binary file
struct info{
char name[10];
int age;
};
int main () {
info ary[2] = { {"howdy", 19},
{"nitey", 12} };
return 0;
}
Reading from a binary file
• Editing from the previous program…
info dest[2];
myfile.open("tmp.txt", ios::in | ios::binary);
myfile.read((char*) dest, sizeof(dest));
int main () {
info source[3] = {"senior", "middle", "junior"};
return 0;
}
Knowledge Check
• Can you