Topic :Introduction To File handling and Input/Output with files in C++ Programming Language:



Introduction To File System:

image: File Operation

Console Application: Console Application means an Application that has a text-based interface. (black screen window)

Until Now we are using Console Oriented Input/output System. so using Console Application, When a Program is terminated, the Program data stored in main memory is lost. For Small Program like calculator, we don't need to store data permanently. but what's about some large programs, where we need to display result of Students, or calculate the Monthly salary of employee.

Whenever we create a Program, and enter any data to process, after the program is terminated, the data stored in the memory will lost. so to store our data Permanent, C++ provide File Handling. This File Handling Provides a Mechanism to store output of a program in a file and read from a file on the disk. So Far, we have been using <iostream> Header file which provide function cin / cout to take input from console and write output to a console respectively. Now, In this Tutorial we are introducing one more header file <fstream>, Which provides data types or classes:



Data Type Description
ofstreamThis is used to create a file and Write data on files.
ifstreamThis is used to read data from files.
fstreamThis is used to both read and write data from/to files.


In Our Tutorial, we will work with objects of fstream type. Class fstream encapsulated both properties of ifstream and ofstream classes. in The case, we want to open file only for input operations, we have to use ifstream object, and if we want to write to file, we have to use ofstream object.





Our Application determines the method we should choose. The access mode of a file determines how we read, write, change, and delete data from the file. Some of our files can be accessed in both ways, Sequentially and randomly as long as our Programs are written properly and the data lends itself to both types of file access.



Sequential Access:

A Sequential file has to be Accessed in the same order the file was written. Let's take an example of Cassette Tapes: We play music in the same order it was recorded. we can Quickly fast-forward or rewind over songs we don't want to listen to, but the order of the songs dictates what we do to play the song we want. But it is difficult, and sometimes impossible, to insert data in the middle of two other songs on a tape. The Only way to truly add or delete records from the middle of a Sequential file is to create a completely New file that combines both old and new records.




It Might seem that Sequential files are limiting, but it turns out that many applications lend themselves to Sequential-file processing.



Random Access:

Unlike Sequential files, we can access Random Access files in any order we want. Think of data in a Random Access file as we would songs on a compact disc or record, we can go directly to any song we want without having to play or fast-forward over the other songs. If we want to play the first song, the sixth song, and then the fourth song, we can do so. The order of play has nothing to do with the order in which the songs were originally recorded. Random-file access sometimes takes more Programming but rewards our effort with a more flexible file-access method.












I Tried my Best to Provide you complete Information regarding this topic in very easy and conceptual way. but still if you have any Problem to understand this topic, or do you have any Questions, Feel Free to Ask Question. i'll do my best to Provide you what you need.

Sardar Omar.
InfoBrother





WRITE FOR INFOBROTHER

Advertising






Advertisement