Streams are a flexible and object-oriented approach to I/O. In this chapter, we will see how to use streams for data output and input. We will also learn how to use. C++ has support both for input and output with files through the following classes: ofstream: File class for writing operations (derived from ostream); ifstream: File. File I/O in C++ works very similarly to normal I/O (with a few minor added complexities). There are 3 basic file I/O classes in C++: ifstream.

Author: Yotaur Akishicage
Country: Timor Leste
Language: English (Spanish)
Genre: Software
Published (Last): 24 June 2012
Pages: 76
PDF File Size: 11.26 Mb
ePub File Size: 9.51 Mb
ISBN: 972-1-58187-731-6
Downloads: 51656
Price: Free* [*Free Regsitration Required]
Uploader: Turn

File handling is as simple as writing in a book, much easier to modify and find. It’s so simple people get confused with it: Welcome to the world of 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 ftream can be retrived later for use. 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 abc to a text file you will see abc 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. The fstream class is derived from the iostream classes, so you can use ofstream variables exactly how you use cout. The fstream class is derived from the iostream classes, so you can use fstream variables how you use cin.

You will tugorial 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 tutoriao the program it will show access fstrea, to the C: Declare them within funtions or classes and close them when their use is over.

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. That wraps up tutoorial 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. The file streams we discussed have a limitation, they can only do input or output at a time.

  ELSEWHERE WILL SHETTERLY PDF

It has great flexibility involving a bit more work with returns being ten fold. Hey, you cant have everything for free what fun would it be if everything was handed to you. These attributes can be used with ifstream and ofstream but of course ios:: 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. Quite an interesting subject tutoriial. It means converting one data type to another.

Here it is converting struct x tytorial a char pointer and it’s address is passed to the funtion. If anybody wants to know more, well tell! Now instead of going more yack yack I show you an example which should tutorlal a lot more. Sigh “A picture is worth a thousand word, source is worth a tutprial bytes”.

Got more doubts now than what was cleared? Good, doubts are the first step towards knowledge. If you happen to be of an opinion that a teacher should explain everything, sorry it’s just not my style. I want thinking students not tape recorders.

Records start at byte zero. I said this once and I will say it again.

Dont forget user friendlyness though. Always know where you are before you start reading or writing to a file or move tutorisl pointer to the area of work. The read pointer and write pointer are separate. So move the correct pointer if you want fstream to work. Always perform checks when necessary or appropriate.

A Gentle Introduction to C++ IO Streams

Remember, with structs and classes their size is always fixed so finding the exact location of a record can be done mathematically. With fstream we work with 2 file pointers, read and write. By moving these pointers we go access any part of the file at random. Both tellp and tellg don’t take any parameters but return where the pointer location in bytes. This is another area people find difficulty.

I figured it out myself without any help what so ever. I also learned file handling a year before my classmates by looking at an elder’s text which contained only vague descriptions so I guess my eagerness to learn had something to do with it. I have always loved computers and programming.

I fell in love with QBasic the moment I saw it. Ah, programming is a part of my soul.

And even when I got my own text book nothing what so ever was mentioned about random file access except a short and vague description about the 2 functions and the options. There was no reference what so ever about locating individual records.

  FAA MEDICAL FORM 8500-8 PDF

I can’t guess why!

Reminds me of a saying “when old, one forgets how it is to be young”. That maybe a cause: I do too when I don’t have much time in the mornings. We should stop this. Never work in the dark without enough light, nor stay up too late. No more than 1: Let start at the beginnings. Ever seen a graph paper?. It has a lot of tiny, small, medium and large sized squares. Also each is being made from the tiniest square. What that got a do with files? Well when you use databases you will almost always use structures and they are always the same size in that particular database.

That means like in the program in Part I all the records are with that one class and when you write it to a file it will still be the same size even if you filled it or not.

It’s like your water bottle. Whether it’s full, half or even empty it will always take up the same space in your bag.

A Gentle Introduction to IO Streams in C++ –

Ok, how will that help us find a record? Well all records are of the same size, they start at zero. They can be thought of as an array too and the concept is similar to how you use normal pointers. A struct is 20 bytes in size. The first rec starts titorial byte zero, the second at byte 20, the next at byte We divide the total file size by the size of the structure to find the number of record.

C++ Tutorials

That’s it you have full knowledge of how to handle fstream. Now all you need is creativity, so use it. I won’t tell you all, so think. Knowledge can be gained from others but wisdom only from yourself and from God. You should have understood what those funtions are for.

Implement them when you do you file handling. Aha, any decent database program must encrypt its files. Just open up any of the files from the example programs with notepad and you will be able to see the text. We definitely don’t want people to see that.

With encryption you can scramble the text so people can read it. Now there are a lot of encryption schemes and a lot of other methods for protecting data.

I will just show you a couple of methods. Another type of encryption. Exclusive-OR encryption, is almost unbreakable through brute force methods.