Syntax of the fseek function
It has the following syntax:
int fseek(FILE *stream, long int offset, int whence);
Parameters
- stream: It represents the pointer to the FILE object.
- offset: It represents the number of bytes to move the file pointer.
- whence: It represents the reference position from where the offset is applied. It can be: SEEKSET: It is used to represent the beginning of the file. SEEKCUR: It represents the current position of the file pointer. SEEK_END: It is used to represent the end of the file.
- SEEK_SET: It is used to represent the beginning of the file.
- SEEK_CUR: It represents the current position of the file pointer.
- SEEK_END: It is used to represent the end of the file.
- Upon successful completion of the operation, the function returns a value of 0.
- In case of failure, the function returns a non-zero value indicating an error, such as an incorrect offset or an invalid pointer.
Example code using the fseek function in C
Let's consider an example to showcase the implementation of the fseek function in the C programming language.
Example
#include <stdio.h>
int main() { //main function
FILE *file;
char ch;
// Open the file in read mode
file = fopen("example.txt", "r");
if (file == NULL) {
printf("Error opening the file.\n");
return 1;
}
// Move the file pointer to the 5th byte from the beginning
fseek(file, 4, SEEK_SET);
// Read the character at that position
ch = fgetc(file);
printf("Character at 5th position: %c\n", ch);
// Close the file
fclose(file);
return 0;
}
Output:
Character at 5th position: n
Explanation:
In this instance, the operation involves opening the file named example.txt in a mode that allows reading. Specifically, the function fseek(file, 4, SEEK_SET); is employed to move the file pointer to the fifth byte position. When the sequence 'Logic Practice' is found within the file, the byte indexing is structured as shown below:
Index: 0 1 2 3 4 5 6 7 8 9
Content: T p o i n t t e c h
Following this, the fgetc(file) method retrieves the character located at the current position, which in this case is 'n'. As a result, 'n' represents the byte at position 4, and subsequently, it gets shown on the screen. Upon completion of the reading process, the file is properly closed and system resources are freed using the fclose function.
By utilizing the fseek function, we have the capability to navigate to any specific location within a file and access the contents directly, without the need to iterate through each character sequentially.
fseek Error Handling in C
In C programming, the fseek function does not raise exceptions as C does not incorporate exception handling similar to C++ programming. Instead, if the operation is unsuccessful, it returns a value other than zero. It is essential to consistently validate the return value to ensure operations are conducted within a design that is resilient to exceptions.
The implementation of the fseek functions is crucial in developing reliable programs, as unhandled errors can result in unpredictable outcomes or data integrity issues.
Error Handling using fseek Example in C
Let's consider an example to illustrate the process of managing errors with the fseek function in the C programming language.
Example
#include <stdio.h>
int main() { //main function
FILE *file;
int result;
// Open file in read mode
file = fopen("example.txt", "r");
if (file == NULL) {
printf("Error: Could not open file.\n");
return 1;
}
// Try to move file pointer to a large offset
result = fseek(file, 10000, SEEK_SET);
if (result != 0) {
printf("Error: fseek failed. Could not move file pointer.\n");
} else {
printf("fseek successful. Pointer moved to position 10000.\n");
}
fclose(file);
return 0;
}
Output:
fseek successful. Pointer moved to position 10000.
Explanation:
In this illustration, we showcase how error management works with the fseek function in C programming. Initially, it opens a file in read-only mode and attempts to reposition the file pointer to byte offset 10000 utilizing the fseek function. If the file lacks sufficient data, the fseek operation is unsuccessful and yields a non-zero result, triggering the display of a suitable error notification.
Conclusion
In the realm of C programming, the fseek function stands out as a crucial asset for facilitating random access to files. This functionality empowers developers to finely adjust the position of the file pointer, granting them meticulous control over where data is read from or written to within a file.
It is also beneficial for bypassing headers, navigating through extensive files without sequential reading, and enabling record updates. Utilizing it in conjunction with various file I/O functions like fgetc, fputc, fread, and fwrite, the fseek function enhances the adaptability and efficiency in file operations.
fseek Function in C FAQs
1) What does the C function fseek do?
Instead of sequentially reading or writing data, the fseek function relocates the file pointer to a designated position within a file to enable random access to data.
The fseek function in C returns zero upon successful execution.
If the operation is executed without errors, it will return 0. In case the operation encounters issues, it will return a value other than zero, usually -1.
3) Which values can be used for the whence parameter in the fseek function?
In C programming, the fseek function accepts three standard constants for the whence parameter:
- SEEK_SET: It is used to represent the beginning of the file.
- SEEK_CUR: It is used to represent the current position of the file pointer.
- SEEK_END: It is used to represent the end of the file.
Yes, the fseek function in C can be applied to both binary and text files.
Nevertheless, due to challenges like newline conversion in textual data, the fseek function is often a more precise and reliable option for managing binary files.
If the cursor is moved beyond the end of the file using the fseek method, it will be positioned at the end of the file.
If the fseek function is employed in write mode, it could potentially result in creating a gap that is filled with null or undefined bytes, enabling additional content to be inserted within that space. The outcome in terms of EOF, errors, or the behavior in read mode can vary based on the specific system and its implementation.