NIOS Computer Science: Chapter 16 – Files Part 3

Download PDF of This Page (Size: 357K)

Image of opening and closing file using open()

Image of Opening and Closing File Using Open()

Image of opening and closing file using open()

2. 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 ifstream or ofstream functions. (The mode is not defined explicitly).

ios : : in for ifstream functions

ios : : out for ofstream 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 :: nocreate

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

ios :: noreplace

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:

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

3. 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.

seekg( ) It moves get pointer to a specified location.

seekp( ) 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 seekg( ) and seekp( ) statement has two parameters.

object.seekg (no. of bytes, refposition);

object.seekp (no. of bytes, refposition);

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

Example 1

infile.seekg(0, ios::beg);

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

infile.seekg(100, ios::cur); It moves the pointer 100 bytes forward from the current position.

infile.seekg(-200, ios::end);

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