The function getw:
The getw function is employed for extracting binary information from a file. It accepts a file stream pointer as its only parameter. The file pointer moves forward based on the data extraction process, typically as a 4-byte integer.
Syntax:
It has the following syntax:
int getw(FILE *stream);
Example: Using getw to read data
Assuming there is a binary file named "file.bin" containing a sequence of integers:
Code:
#include <stdio.h>
int main() {
FILE *file;
int data;
file = fopen("file.bin", "rb");
if (file == NULL) {
perror("Error opening the file");
return 1;
}
while ((data = getw(file)) != EOF) {
printf("%d ", data);
}
fclose(file);
return 0;
}
Output:
Suppose the binary information stored in the "file.bin" document consists of the values 10, 20, 30, 40, and 50.
The program's output will be:
10 20 30 40 50
Explanation:
- In this example, the required header file h is included, which contains the getw function's definition.
- A file pointer file and an integer variable called data are declared to hold the information read from the file.
- Using fopen, the binary file "file.bin" is opened in read mode ("rb") .
- 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.
- Each integer is printed using printf with the format specifier %d during the loop.
- Finally, fclose is used to close the file.
The putw Method
The putw function is employed to output binary information to a file. It necessitates the data to be written and a reference to the file stream where the data will be written. Typically, the data is recorded in the file as a 4-byte integer.
Syntax:
It has the following syntax:
int putw(int data, FILE *stream);
Example: Using putw to write data
Let's consider a program that utilizes the putw function to write a sequence of integers to a binary file:
Code:
#include <stdio.h>
int main() {
FILE *file;
int data[] = {10, 20, 30, 40, 50};
int i;
file = fopen("file.bin", "wb");
if (file == NULL) {
perror("Error opening the file");
return 1;
}
for (i = 0; i<sizeof(data) / sizeof(data[0]); i++) {
putw(data[i], file);
}
fclose(file);
return 0;
}
Output:
A binary file titled "file.bin" will be generated upon program execution. The integers stored in the data array will be converted into binary format within this file:
10 20 30 40 50.
Explanation:
- In this example, a data integer array with a few values is declared.
- A file pointer file is declared.
- Using fopen , the binary file "file.bin" is opened in write mode ("wb") .
- We iterate through the data array using a for loop and the putw function to write each element to the file.
- Finally, we use fclose to close the file.
- 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.
- The compatibility of data formats must be ensured while using these functions.
- Additionally, using binary files requires extra attention since data corruption could result from improper file handling .
- After reading or writing , always properly close the files, and refrain from combining binary and text modes when performing operations.
- These safety measures will result in effective and secure file handling in C.
Some importanLogic Practices:
Conclusion
In this post, we explored the C functions readw and writew for handling binary data input and output in file operations. These functions are beneficial for managing extensive datasets or preserving the binary format of information. It is crucial to manage file operations diligently by correctly opening and closing files to avoid any potential issues.