Unformatted input and output in C involve reading and writing data without specifying any particular format. Unlike formatted input and output, which use format specifiers to control the appearance and interpretation of data, unformatted I/O treats data as raw bytes without any formatting. In C, unformatted I/O is typically performed using functions like fread(), fwrite(), fgetc(), and fputc(). Let’s explore unformatted input and output in detail:
Unformatted Output with fwrite():
The fwrite() function is used to write unformatted data to a file or output stream. It takes the following arguments:
- A pointer to the data to be written.
- The size of each data item (in bytes).
- The number of data items to be written.
- The output stream.
Example:
int data[] = {1, 2, 3, 4, 5};
FILE *file = fopen(“data.bin”, “wb”);
fwrite(data, sizeof(int), 5, file); // Writing 5 integers to the file
fclose(file);
Unformatted Input with fread():
The fread() function is used to read unformatted data from a file or input stream. It takes the following arguments:
- A pointer to the buffer where the data will be stored.
- The size of each data item (in bytes).
- The number of data items to be read.
- The input stream.
Example:
int data[5];
FILE *file = fopen(“data.bin”, “rb”);
fread(data, sizeof(int), 5, file); // Reading 5 integers from the file
fclose(file);
Unformatted Character Input and Output:
fgetc():
- Reads a single character from a file or input stream.
- Returns the character read as an int, or EOF (end-of-file) if an error occurs or the end of the file is reached.
fputc():
- Writes a single character to a file or output stream.
- Takes a character (as an int) and the output stream as arguments.
- Returns the character written if successful, or EOF if an error occurs.
Example:
FILE *input = fopen(“input.txt”, “r”);
FILE *output = fopen(“output.txt”, “w”);
int ch;
while ((ch = fgetc(input)) != EOF)
{
fputc(ch, output);
}
fclose(input);
fclose(output);
Benefits of Unformatted I/O:
- Efficiency: Unformatted I/O is often more efficient than formatted I/O because it involves less processing overhead.
- Binary Data Handling: Unformatted I/O is well-suited for handling binary data, as it does not impose any specific format on the data.
- Low-Level Manipulation: Unformatted I/O provides low-level access to data, allowing for more precise control over file operations.
Considerations:
- Portability: Unformatted I/O may be less portable than formatted I/O, as it deals with raw binary data that may not be compatible across different systems.
- Debugging: Unformatted data may be harder to interpret and debug compared to formatted data, especially when dealing with complex data structures.
Conclusion:
Unformatted input and output in C provide a low-level mechanism for reading and writing data without any specific formatting. While it may be less user-friendly than formatted I/O, unformatted I/O is efficient and well-suited for handling binary data and low-level file operations. Understanding how to use functions like fwrite() and fread() effectively is essential for performing unformatted I/O operations in C programs.