C Programming Language Tutorial
Variables and Data Types
Input/Output
Looping and Selection Structures
Array
Functions
Preprocessing Command
Pointer
Structure
File Operations
Important Knowledge
In this tutorial, we will learn about random read and write operations in files using the rewind()
and fseek()
functions in the C programming language.
rewind() function:
The rewind()
function is used to set the file position to the beginning of the file. This function takes a pointer to a FILE
structure as its argument. The rewind()
function is equivalent to calling fseek()
with an offset of 0 and SEEK_SET
as its origin.
#include <stdio.h> #include <stdlib.h> int main() { FILE *file = fopen("example.txt", "r"); if (file == NULL) { perror("Error opening file"); return EXIT_FAILURE; } // Read and print a character from the file int ch = fgetc(file); printf("Character: %c\n", ch); // Reset the file position to the beginning rewind(file); // Read and print the character again ch = fgetc(file); printf("Character after rewind: %c\n", ch); fclose(file); return EXIT_SUCCESS; }
fseek() function:
The fseek()
function is used to change the file position to a specific location in the file. It takes three arguments:
FILE
structure.long int
representing the offset (number of bytes) from the specified origin.int
specifying the origin from which the offset should be applied. It can have one of the following values:SEEK_SET
: Beginning of the file.SEEK_CUR
: Current position of the file pointer.SEEK_END
: End of the file.#include <stdio.h> #include <stdlib.h> int main() { FILE *file = fopen("example.txt", "r"); if (file == NULL) { perror("Error opening file"); return EXIT_FAILURE; } // Move the file position 10 bytes from the beginning fseek(file, 10, SEEK_SET); // Read and print a character from the file int ch = fgetc(file); printf("Character after fseek: %c\n", ch); // Move the file position 5 bytes back from the current position fseek(file, -5, SEEK_CUR); // Read and print the character ch = fgetc(file); printf("Character after moving back: %c\n", ch); fclose(file); return EXIT_SUCCESS; }
Note: When using fseek()
with text mode files, it's safe to use the SEEK_SET
origin with an offset returned by the ftell()
function. However, using SEEK_CUR
or SEEK_END
with arbitrary offsets may produce unexpected results due to line endings translation.
In summary, the rewind()
and fseek()
functions in C programming language are used for random read and write operations in files. The rewind()
function sets the file position to the beginning of the file, while the fseek()
function allows you to change the file position to a specific location with a specified offset and origin.
Using rewind() for file pointer reset in C:
rewind()
function in C is used to reset the file position indicator to the beginning of the file. It is mainly used with files opened in text mode.#include <stdio.h> int main() { FILE *file = fopen("example.txt", "r"); if (file != NULL) { // Read operations here // Reset file pointer to the beginning rewind(file); // Additional read operations after rewind fclose(file); } else { printf("Error opening the file.\n"); } return 0; }
Seeking to specific positions with fseek() in C:
fseek()
function allows you to set the file position indicator to a specific position in the file. It's commonly used for both text and binary files.#include <stdio.h> int main() { FILE *file = fopen("example.txt", "r"); if (file != NULL) { // Read operations here // Set file pointer to a specific position (e.g., 50 bytes from the beginning) fseek(file, 50, SEEK_SET); // Additional read operations after fseek fclose(file); } else { printf("Error opening the file.\n"); } return 0; }
Random reading from files using fseek() in C language:
#include <stdio.h> int main() { FILE *file = fopen("example.txt", "r"); if (file != NULL) { // Set file pointer to a specific position fseek(file, 30, SEEK_SET); // Read data from the current position fclose(file); } else { printf("Error opening the file.\n"); } return 0; }
Random writing to files with fseek() in C programming:
fseek()
for random writing by setting the file pointer to a specific position and then writing data.#include <stdio.h> int main() { FILE *file = fopen("example.txt", "r+"); if (file != NULL) { // Set file pointer to a specific position fseek(file, 50, SEEK_SET); // Write data to the current position fclose(file); } else { printf("Error opening the file.\n"); } return 0; }
Handling binary files with random access in C:
fseek()
and fread()
or fwrite()
to navigate and manipulate data in binary files.#include <stdio.h> int main() { FILE *file = fopen("binary_file.bin", "rb+"); if (file != NULL) { // Set file pointer to a specific position fseek(file, 2 * sizeof(int), SEEK_SET); // Read or write binary data at the current position fclose(file); } else { printf("Error opening the binary file.\n"); } return 0; }