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 : What is an Operator Operator : It is a

    Operator: It is a symbol, like -, = or ?: taking one, two or three operands and yielding an outcome. The operators are employed in both arithmetic and Boolean expressions.

  • Q : What is Bit Bit : It is a binary digit

    Bit: It is a binary digit that can take on two possible values: 0 and 1. The bits are basic building block of both data and programs. Computers regularly shift data around in multiples of eight-bit units (that is, bytes for the sake of effectiveness).

  • Q : Programming assignment Written (7

    Written (7 points): pp. 303-304: 1, 5, 6, 7 pp. 363-364: 3, 4, 6. pp.303-304: 1. In what ways are the initialization, repetition test, and update steps alike for a sentinel-controlled loop and an endfile-controlled loop? How are they different? 5. Rewrite the program

  • Q : Explain Branch instruction Branch

    Branch instruction: It stores a new instruction address into the program counter. The consequence of this is the next instruction to be fetched will not generally be the one instantly following the branch instruction. Therefore the normal chronologica

  • Q : Define the way to threads own the mutex

    Define the way to threads own the mutex?

  • Q : Write a recursive function intpower(base

    Write a recursive function intpower(base, exponent) that when invoked returns base^exponent. For example, intpower(3,4) = 3*3*3*3. Assume that the exponent is an integer greater than or equal to 1.

  • Q : Define Unconditional Jumps

    Unconditional Jumps: Jumps which are not strictly upwards in the block hierarchy can require extensive control-flow manipulation, including creation of redundant code, and should be avoided if possible. Such jumps add considerable modeling overhead fo

  • Q : Define HyperText Markup Language

    HyperText Markup Language: The HyperText Markup Language (abbreviated as HTML) is a simple presentation language employed to markup the content of the Web pages. Its tags appear frequently in pairs to mark sections of text which must be represented in

  • Q : Define Data type Data type : There are

    Data type: There are 8 primitive data types in Java; five of such symbolize numerical types of changeable range and precision - float, double, int, long and short. The remaining three are employed to symbolizing single-bit values (that is, Boolean), s

  • Q : Explain the good example of XHTML

    Explain the good example of XHTML element opening tags.

©TutorsGlobe All rights reserved 2022-2023.