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 Method overloading Method

    Method overloading: Two or more techniques with similar name stated within a class are said to be overloaded. This exerts to both constructors and other methods. The overloading applies via a class hierarchy, thus a sub class may overload a method sta

  • 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 : What do you mean by ActiveX control

    What do you mean by ActiveX control? Describe in brief.

  • Q : Explain Package declaration Package

    Package declaration: It is a declaration employed to name a package. This should be the first item in the source file, preceding any import statements. For example,     pa

  • Q : OCIW32 dll error Normal 0 false false

    Normal 0 false false

  • Q : Define Finite State Machines Finite

    Finite State Machines : A Finite State Machine (FSM) is one of the most suitable models for formal checks, especially for concurrent systems. However, FSMs can have problems with inheritance (the state model can change in derived classes) if state asp

  • Q : Define Race hazard Race hazard : It is

    Race hazard: It is a situation which occurs between multiple threads sharing a resource. The race hazard occurs whenever one thread's suppositions regarding the state of a resource are invalidated by the actions of the other thread.

  • Q : State the term XLL State the term XLL?

    State the term XLL? Answer: The specification of XML Link Language has XPointer or XLink.

  • Q : What is Super class Super class: It is

    Super class: It is a class which is extended by one or more sub classes. All Java classes encompass the Object class as a super-class.

  • Q : Describe Multiple-boot options

    Multiple-boot options: The hardware configurations of several computers are capable to run various operating system and window manager combinations. A few systems permit a user to select which combination they wish to utilize during a specific session

©TutorsGlobe All rights reserved 2022-2023.