Getw() and putw() function in C
In the C programming language, the putw() and getw() are file-handling functions used to write values into and read them out of files, respectively.
Definition:
putw(): The putw() function is used to write a value into a file. We can write the below value into a file using a C program.
putw(i, fp);
Here, i – integer value
fp – file pointer
Syntax: int putw(int number, FILE *fp);
getw(): The getw function reads a value from the file that fp is pointing at. We can read the data below from a file using the C program.
getw(fp);
Syntax: int getw(FILE *fp);
Let us see an example program for clarity:
Code:
#include <stdio.h>
int main ()
{
FILE *fp;
int i, num;
printf("Enter value\n");
scanf("%d",&i);
fp = fopen ("tae.txt", "w");
putw(i,fp);
i++;
putw(i,fp);
fclose(fp);
fp = fopen ("tae.txt", "r");
while(getw(fp)!=EOF)
{
num= getw(fp);
printf(“Data in tae.txt file is %d \n”, num);
}
fclose(fp);
return 0;
}
Output:
![Getw() and putw() function in C](https://static.tutorialandexample.com/c/getw-and-putw-function-in-c1.png)
Applications of getw() and putw():
There are various applications of getw() and putw() function. Some of the main applications of these functions are as follows:
Non-Portable Binary Data: The getw() and putw() functions may be used when portability is not a priority, and you need to write or read binary data closely tied to a particular architecture or operating system.
Performance Optimization: Due to their word-based I/O operations, getw() and putw() may occasionally outperform fread() and fwrite() for big datasets, especially on specific architectures.
Legacy Data Formats: The getw() and putw() may be appropriate if you need to work with older binary data formats that contain integers in a particular machine's binary format, and you are sure that the data files will only be utilized on that machine type.
It's critical to stress those hazards and restrictions are associated with employing these features. Due to the non-standard nature of these functions, behaviour may differ between platforms and compilers. Your code may not be as portable because some systems might only support it. It is generally advised to avoid using getw() and putw() in new code and choose more adaptable and dependable substitutes instead, such as fread() and fwrite().
Drawbacks of using getw() and putw():
There are several drawbacks of getw() and putw() function. Some of the main drawbacks are as follows:
Not Part of Standard Library: Getw() and putw() are not included in the C standard library; therefore, how they behave may differ depending on the platform and compiler being used. Your code may not be as portable if all operating systems don't support it.
Non-portability: This feature's major downside is its immobility. These operations read and write data in the native binary format of the computer. A separate machine with a different architecture may not be able to read or understand files written using putw() as a result. Your code and data's mobility is constrained, making file sharing across platforms challenging.
Security Issues: Because these functions read and write binary data directly, security flaws such as buffer overflows or accidental data exposure are dangerous if it is not utilized carefully.
Lack of Flexibility: The functions getw() and putw() only work with integers, which are typically 4 bytes in size. You must manually create additional serialization and deserialization logic if you need to read or write data of other sorts, such as floating-point numbers, structures, or custom data types.
Poor Performance: Although reading and writing data as words may seem more effective, recent compilers and file I/O implementations have greatly improved. In reality, there is not much of a speed advantage to utilizing getw() and putw() over fread() and fwrite(), because the disadvantages exceed any prospective performance advantages.
Limited Error Handling: After there are errors or after the file has finished, the methods return EOF (End-of-File). However, they need to offer specific error codes or ways to deal with different mistakes, making it more challenging to identify and fix potential problems.
Conclusion:
In conclusion, in contemporary C programming, utilizing getw() and putw() has more disadvantages than benefits. It is advised to utilize more portable and dependable alternatives for binary file I/O, such as fread() and fwrite(), combined with appropriate serialization/deserialization methods for handling complicated data structures.