Understanding Disk Input and Output Functions in C
Disk input and output (I/O) functions in C are essential for managing file operations such as reading from and writing to files on the disk. These functions are part of the standard I/O library stdio.h, which provides programming interfaces for handling various file operations effectively. This article will cover the key disk I/O functions and demonstrate their usage with an example.
Key Functions Overview
File Opening and Closing
The process of opening and closing files involves specific functions which will be detailed below.
FILE *fopen(const char *filename, const char *mode): Opens a file and returns a pointer to a FILE object, which identifies the stream. The filename argument specifies the name of the file to be opened, whereas the mode argument determines the file's opening mode (read, write, append, etc.). int fclose(FILE *stream): Closes an opened file.Reading from Files
Reading data from a file is performed using various functions that read either a single character, a line, or a block of binary data.
int fgetc(FILE *stream): Reads a single character from the file. char *fgets(char *str, int n, FILE *stream): Reads a line from the file, stopping at a newline or end-of-file (EOF). size_t fread(void *ptr, size_t size, size_t count, FILE *stream): Reads a sequence of binary data from the file.Writing to Files
Writing data to a file is achieved through the following functions.
int fputc(int c, FILE *stream): Writes a single character to the file. int fputs(const char *str, FILE *stream): Writes an entire line (string) to the file. size_t fwrite(const void *ptr, size_t size, size_t count, FILE *stream): Writes a sequence of binary data to the file.File Positioning
Positioning the file pointer within a file is crucial for accessing specific parts of a file. The following functions are used for this purpose:
int fseek(FILE *stream, long offset, int whence): Moves the file pointer to a specified location in the file. The whence argument can be SEEK_SET, SEEK_CUR, or SEEK_END. long ftell(FILE *stream): Returns the current position of the file pointer within the file. void rewind(FILE *stream): Sets the file position indicator to the beginning of the file.Error Handling
Handling errors and checking for end-of-file conditions is important for robust file management. The following functions are used for error handling.
int EOF: Checks for the end-of-file indicator. int ferror(FILE *stream): Checks for a file error.Example Usage
The following example shows how to use some of the disk I/O functions in a simple C program.
First, we include the necessary header file: Then, the main function is defined: The file is opened in write mode: Data is written to the file using fputs: The file is closed: The file is then opened in read mode: Data is read from the file using fgets: The file is closed again: Finally, the program returns 0.#include stdio.hint main() { FILE *file; char buffer[100]; // Open a file for writing file fopen("example.txt", "w"); if (file NULL) { perror("Failed to open file for writing"); return 1; } // Write to the file fputs("Hello, world! ", file); fclose(file); // Open the file for reading file fopen("example.txt", "r"); if (file NULL) { perror("Failed to open file for reading"); return 1; } // Read from the file while (fgets(buffer, sizeof(buffer), file) ! NULL) { printf("%s", buffer); } fclose(file); return 0;}
Summary
Disk I/O functions in C provide a comprehensive and reliable way to manage file operations, including opening, reading, writing, and closing files. Mastery of these functions is essential for effective file handling in C programming. By understanding and utilizing these functions, developers can create efficient and robust file management systems in C.