Files: Opening and Closing File Using Open, Open Function and File Pointers

Get unlimited access to the best preparation resource for IAS : Get complete video lectures from top expert with unlimited validity: cover entire syllabus, expected topics, in full detail- anytime and anywhere & ask your doubts to top experts.

Download PDF of This Page (Size: 209K)

Image of opening and closing file using open()

Image of Opening and Closing File Using Open()

Image of opening and closing file using open()

Open () Function

The open () function has two parameters: filename and access mode. The general format is:

Stream _object. open (“filename”, access mode).

The second argument specifies the mode in which the file is opened. The default values are taken for if stream or of stream functions. (The mode is not defined explicitly).

ios: in for if stream functions

ios: out for of stream functions

The file mode parameters can take one or more of the constants defined in the class ios. The following table shows the file mode parameters.

The Following Table Shows the File Mode Parameters.
The following table shows the file mode parameters.

Parameter

Meaning

ios: app

It opens the file in output mode. The file pointer is at the end of file and it can add a record.

ios: ate

The file pointer is at the end of the file and it allows to add data or to modify the existing data anywhere in the file.

ios: binary

Binary file

ios: in

It opens the file in input mode. The file pointer is at the top of the file and it is ready for reading.

ios: no create

If file is already present, it opens the file otherwise open fails.

ios: no replace

If file is not present, it opens the file otherwise open statement fails.

ios: out

It opens the file in output mode. The file pointer is at the end of the file. If it already has a data, the output mode erases the content of the file.

ios: trunk

It deletes the contents of the file if exist

The mode can combine two or more parameters using bitwise OR operator.

Example:

Out file. open (“ABC”, ios::in l ios::out l ios :: binary);

File Pointers

File has two associated pointers called input pointer (or get pointer) and output pointer (or put pointer).

Each time an input or output operation takes place, the pointer moves automatically. There are two pointers.

See kg () It moves get pointer to a specified location.

See kp () It moves the put pointer to a specified location.

A file can be viewed as

Image of Pointer to a specified location

Image of Pointer to a Specified Location

Image of Pointer to a specified location

ios: beg means start of the file

ios: cur means current position of the pointer

ios: end means end of the file.

The see kg () and see kp () statement has two parameters.

Object. See kg (no. of bytes, reposition).

Object. See kp (no. of bytes, reposition).

The reposition takes one of the above three constants defined in the ios class.

Example 1

In file. See kg (0, ios: beg).

It moves the pointer to the beginning of the file. In this case, the reposition ios: beg is optional.

In file. see kg (100, ios: cur); It moves the pointer 100 bytes forward from the current position.

In file. See kg (-200, ios: end).

It moves the pointer 200 bytes backward from the end of the file.

Developed by: