File I and Streams
File I and Streams
Intro
We will use the c++ fstream classes to do our file handling. So, what is a file? A file is just a
bunch of bytes stored on a hardisk. Some have a specific structure others dont. Files are used to
save info so that it can be retrived later for use. [I dont think you will want to save 100 people's
address in memory will you].
Types of Files
Actually there are only two. Text files and binary files.
In text files data is stored as readable chars and binary file are in machine language. So if you
output abc123 to a text file you will see abc123 but in a binary file you may see only a bunch of
black blocks if you use notepad. The binary files are smaller in size.
fstream.h
fstream.h provides simultaneous input and output through ifstream, ofstream and fstream.
ifstream - open the file for input
ofstream - open the file for output
fstream - open the file for input/output/both
Writing to a file
Relatively very simple.
Steps:
Declare an ofstream var.
Open a file with it.
Write to the file (there are a couple of ways.)
Eg Program 1.1
#include <fstream.h>
void main
{
ofstream file;
file.close(); //close it
}
file<<"string\n";
file.put('c');
void main
{
ifstream file;
char output[100];
int x;
file>>output; //write to it
cout<<output; //result = Hello file
file>>x;
cout<<x; //result = 75
file.close(); //close it
}
You will use the constructor for opening a file.I would not recommend using this not because it
works less well or anything but in most cases it will improve code clarity and prevent errors
when you are handling multiple files. If a file handle is used more than once without calling a
close() in between them there will be errors. This is just provided for info sake if you need to use
it in a hurry or in something small.
Never ever declare a fstream variable globally. It is a bad habit. If you forget to close it next time
you run the program it will show access errors to the C: drive (or which ever drive you use) and
you will have to restart the computer. Declare them within funtions or classes and close them
when their use is over.
If you are doing databases or any file handling for that matter never put any file i/o into classes.
It will simply complicate debugging and you may also open a single file multiple times with
difffrent objects at the same time. This is definitely not what we want. Classes are only to be
used when you have to minimal file i/o but still I recommend you use normal funtions.
ifstream stands for input stream and can only be used for input.
ofstream stands for output stream and can only be used for output.
Any variable declared with ifstream,ofstream or fstream is called a file handle.
That wraps up the simple very stuff. You will not use them much unless you are working with
text files or in a small project. Now we will move on to fstream which is more flexible and will
be most used. It's easy if you look at it logically.
file.open("file.ext",iso::in|ios::out)
file.close();
}
Notice anything new? The ios::--- are attributes which define how a file should be opened.
List of Attributes
ios::in open file for reading
ios::out open file for writing
ios::app open for writing,add to end of file(append).
ios::binary Binary file
ios::nocreate do not create the file,open only if it exists
ios::noreplace open and create a new file if the specified file does not exist
ios::trunc open a file and empty it.(Boom, all the data is gone,if any)
ios::ate goes to end of file instead of the begining
Notes
Until now we have only been able to use strings and ints to write/read. Most databases will want
to store data in structures or classes. If we had to write a seperate function to split and write each
member, brrr horrors. C++ goes to be very nice and provides us with a way to write entire
classes or structures with little work
struct x
{
int i;
char a;
char s[10];
}data;
file.write((char*)&data,sizeof(x));
file.read((char*)&data,sizeof(x));
The rest is simple. You pass the size of the structure which can be found out by using sizeof();
Eg:
cout<<"\nInt:"<<sizeof(int);
cout<<"\nFloat:"<<sizeof(float);
cout<<"\nChar:"<<sizeof(char);