Ofstream stands for output stream and can only be used for output. Ifstream stands for input stream and can only be used for input. Classes are only to be used when you have to minimal file i/o but still I recommend you use normal funtions. It will simply complicate debugging and you may also open a single file multiple times with difffrent objects at the same time. If you are doing databases or any file handling for that matter never put any file i/o into classes. Declare them within funtions or classes and close them when their use is over. 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.
![difference between istream and ifstream difference between istream and ifstream](http://www.cs.sjsu.edu/~pearce/modules/lectures/pop/app1/image489.gif)
Never ever declare a fstream variable globally. This is just provided for info sake if you need to use it in a hurry or in something small. If a file handle is used more than once without calling a close() in between them there will be errors.
Difference between istream and ifstream code#
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.
![difference between istream and ifstream difference between istream and ifstream](https://lh5.googleusercontent.com/-PhTPZLHNmxE/AAAAAAAAAAI/AAAAAAAAAOQ/LCxU1sb1MO4/photo.jpg)
In text files data is stored as readable chars and binary file are in machine language. Files are used to save info so that it can be retrived later for use.Īctually there are only two. Some have a specific structure others dont. So, what is a file? A file is just a bunch of bytes stored on a hardisk. We will use the c++ fstream classes to do our file handling. It's so simple people get confused with it :-). File handling is as simple as writing in a book, much easier to modify andįind.