Reads and writes functions

To accumulate or recover data in binary form, the member functions write () or read () can be utilized. Unlike put () and get (), the write () and read () functions access data in binary arrangement. In binary format, the data representation in the system and in the file is similar. The number of bytes required to represent an integer in text from is proportional to its magnitude, where as in binary form the size is always fixed irrespective to its magnitude. Thus the binary form is more accurate, and provides faster access to the file because no conversion is required while performing read or writes. The read () and write () functions have the subsequent syntax: -

In file. Read ((char *) & variable, size of (variable));

Out file. Write ((char *) & variable, size of (variable));

The first parameter is a pointer to a memory location at which the data retrieved from the file is to be stored in case of read () and address at which data is to be written when retrieved from a file in case of write (). The second parameter indicates the number of bytes to be transferred and the programme give below illustrates the certain and manipulation of binary files.

Use of write and read member of file steams:

# include < f stream. h >

   Void main ()

{

Int num 1 = 530;

Float num 2 = 1050.25;

// open file in write binary mode, write integer and close.

Of stream out _ file (num. Binary", ios : : binary);

 Out _ file. Write ((char *) & num 1, size of (num 1));

 Out _ file. Write ((char *) & num 2, size of (num 2));

Out _ file. Close ();

If stream in _ file ("number. Binary", ios : : binary);

In _ file. Read ((char *) & num 1, size of Int));

In _ file. Read ((char *) & num 1, size of (num 2));

  Cout << num 1 << " " << num 2 << end 1;

In _ file . close ();

}

   Related Questions in Programming Languages

  • Q : Explain the way to start a Web Service

    Explain the way to start a Web Service.

  • Q : What is Final method Final method :

    Final method: This is a method with the final reserved word in its header. The final method might not be overridden by a method stated in a sub class.

  • Q : Explain Multiple inheritance Multiple

    Multiple inheritance: The capability of a class or interface to expand more than one interface or class. In Java, multiple inheritance is only accessible in the circumstances which are shown below: An interf

  • Q : Explain way to open one resistance of

    Explain the way to open just one resistance of vb application (.exe).

  • Q : Define undershoot You have a driver as

    You have a driver as drives a long signal and connect to an input device. On the input device there is either undershoot, overshoots or signal threshold violations, so what can be done to correct such problem?

  • Q : Explain shell variables Explain the

    Explain the shell variables?

  • Q : Different layers in Windows Architecture

    Explain how many different layers comprise in Windows Architecture?

  • Q : Explain Inheritance hierarchy

    Inheritance hierarchy: The relationship among super-classes and sub-classes is termed as an inheritance hierarchy. The single inheritance of classes means that each and every class has simply a single `parent' class and that Object class is the eventu

  • Q : Define Object-oriented language

    Object-oriented language: Programming languages like Java and C++ which permit the solution to a trouble to be stated in terms of objects that belong to the classes.

  • Q : Modeling Language for SPIN-Promela

    Modeling Language: The modeling language used for SPIN is called Promela (Process Meta Language). In fact, the name SPIN stands for Simple Promela Interpreter. Promela is a powerful C-like specification language with a variety of synchronization primi

©TutorsGlobe All rights reserved 2022-2023.