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 : Method over ridding in java Q.  

    Q. Explain method over ridding in java. Give ex

  • Q : Hide sections of a course from students

    Normal 0 false false

  • Q : What is an Object Object : It is an

    Object: It is an instance of a particular class. In common, any number of objects might be constructed from a class definition. The class to which an object belongs states the common characteristics of all instances of that class. In those characteris

  • Q : Explain Polling Polling : It is the

    Polling: It is the process of repeatedly testing until a situation becomes true. Polling can be incompetent when the time between tests is little compared with the time it will take for the condition to become true or right. A polling thread must slee

  • Q : Define Fully evaluating operator Fully

    Fully evaluating operator: An operator which computes all of its arguments to generate an outcome. Standard arithmetic operators, like +, are totally evaluating. In contrary, some Boolean operators, like &&, are short-circuit operators.

  • Q : Protocol stack of XML Web Services

    Explain the protocol stack of XML Web Services.

  • Q : Define the term Instance Define the

    Define the term Instance: It is a synonym for object. The objects of a class are instantiated whenever a class constructor is invoked through the new operator.

  • Q : What is Pointer Arithmetic Pointer

    Pointer Arithmetic: C and C++ provide the ability to modify a pointer’s target address with arithmetic operations. This is used, for example, to index arrays. MyObject* P = ...

    Q : Describe Real number Real number : It

    Real number: It is a number with an integer and a fractional portion. The primitive types double and float are employed to symbolize real numbers.

  • Q : Function of STR and LPSTR Explain what

     Explain what is the function of STR and LPSTR?

©TutorsGlobe All rights reserved 2022-2023.