Javatpoint Logo
Javatpoint Logo

Getw() and Putw() function in C

Introduction

Reading and writing data to external files in the C programming language need careful file handling. Two of the functions provided by the standard I/O library to communicate with files are getw() and putw() functions. These routines are important for effectively managing enormous datasets because they can read and write binary data to files. In this article, we will examine getw() and putw() function in-depth, along with code snippets and usage descriptions.

The function getw():

The getw() function is used to read binary data from a file. A pointer to the file stream from which the data is to be read serves as its sole argument. The file pointer is advanced in accordance with how the function reads the data, which is normally read as an integer (4 bytes).

Syntax:

It has the following syntax:

Example: Using getw() to read data

Assuming we have a binary file called "file.bin" that has a list of integers in it:

Code:

Output:

Let's say the binary data in the "file.bin" file is 10 20 30 40 50.

The program's output will be:

10 20 30 40 50

Explanation:

  1. In this example, the required header file h is included, which contains the getw() function's definition.
  2. A file pointer file and an integer variable called data are declared to hold the information read from the file.
  3. Using fopen(), the binary file "file.bin" is opened in read mode ("rb").
  4. After that, the getw() function is used in a while loop to read numbers from the file until the end-of-file (EOF) is reached. The variable data contains the read data.
  5. Each integer is printed using printf() with the format specifier %d during the loop.
  6. Finally, fclose() is used to close the file.

The putw() Method

The putw() function is used to write binary data to a file. The data to be written and a pointer to the file stream where the data is to be written are its two required inputs. The data is written to the file as an integer, which is normally 4 bytes long.

Syntax:

It has the following syntax:

int putw(int data, FILE *stream);

Example: Using putw() to write data

Let's take a program that uses putw() to write a series of integers to a binary file:

Code:

Output:

A binary file named "file.bin" will be produced after running the program. The integers from the data array will be represented in binary form in this file:

10 20 30 40 50.

Explanation:

  1. In this example, a data integer array with a few values is declared.
  2. A file pointer file is declared.
  3. Using fopen(), the binary file "file.bin" is opened in write mode ("wb").
  4. We iterate through the data array using a for loop and the putw() function to write each element to the file.
  5. Finally, we use fclose() to close the file.

Some important points:

  1. In addition to being helpful for managing binary data, the getw() and putw() functions need to be utilised cautiously. Due to variations in binary data formats, some functions are not system independent.
  2. The compatibility of data formats must be ensured while using these functions.
  3. Additionally, using binary files requires extra attention since data corruption could result from improper file handling.
  4. After reading or writing, always properly close the files, and refrain from combining binary and text modes when performing operations.
  5. These safety measures will result in effective and secure file handling in C.

Conclusion

In this article, we examined the C methods getw() and putw() for reading and writing binary data to files. These functions can be helpful when working with huge datasets or maintaining the binary representation of data. Always handle file operations carefully, ensuring files are opened and closed correctly to prevent any problems.


Next Topiclseek() in C





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA