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 Append mode Append mode : It is

    Append mode: It is a file writing mode, in which the existing contents of a file are maintained whenever the file is opened. Novel contents are appended to the existing.

  • Q : Does TestComplete maintain testing of

    Does TestComplete maintain testing of Flex applications?

  • Q : State Default label Default label : The

    Default label: The destination for all values employed in a switch statement expression which do not contain explicit case labels. The default label is not obligatory.

  • Q : State the term a markup language State

    State the term a markup language?

  • Q : Define the term RGB Color Model Define

    Define the term RGB Color Model: It is a color model based on representing a color as three components: green, red, and blue.

  • Q : What is an Arithmetic expression

    Arithmetic expression: It is an expression comprising numerical values of integer or floating point kinds. For example, operators like +, -, *, / and % get arithmetic expressions as their operands and generate arithmetic values as their outcomes.

  • Q : Various controls of ASP.NET AJAX Name

    Name the various controls of ASP.NET AJAX?

  • Q : Tower of Hanoi Puzzle program using C#

    Tower of Hanoi Puzzle program using C# and Windows Presentation Foundation (WPF) template in Visual Studio 2012 or newer.

  • Q : Explain Call-by-value Call-by-value:

    Call-by-value: The semantics of passing an argument to a method in which a copy of actual argument value is taken and positioned in a separate memory location, symbolized by the corresponding formal argument. As an outcome, assignment to the formal ar

  • Q : Search and Partial Coverage of Java

    Search and Partial Coverage: JPF supports well-known search strategies such as BFS and DFS as well as various heuristic-based searches such as A*, Best-First, and Beam Search. You can sum a number of heuristics during the search, and set search depth

©TutorsGlobe All rights reserved 2022-2023.