In this article, we’ll take a look at using the fseek() function in C/C++.
fseek() is a very useful function to traverse through a file. We can ‘seek’ to different locations, by moving the file pointer.
This enables us to control where we can read and write to and from files.
Let’s take a look at using this functions, using some illustrative examples!
Basic Syntax of fseek() in C/C++
The fseek()
function will move the file pointer to a file, based on the option that we give it.
This function is present in the <stdio.h>
header file.
The prototype of the function is as follows:
#include <stdio.h>
int fseek(FILE* fp, int offset, int position);
Usually, if we are moving the pointer, we need to specify the starting position (offset
) from which it will move!
There are three options for choosing position
, from where you can use offset
to shift the pointer.
Here, position
can take the following macro values:
- SEEK_SET -> We place the initial position at the start of the file, and shift from there.
- SEEK_CUR -> The initial position is taken at the current position of the existing file pointer.
- SEEK_END -> We place the initial position at the end of the file. If you shift the pointer from this position, you will reach
EOF
.
If the function executes successfully, it will return 0. Otherwise, it will return a non-zero value.
NOTE: In case of SEEK_END
, the offset
position is measured backwards, so we’ll be moving from the end of the file!
For example, if you try to seek to a position which doesn’t exist, it will fail!
Now that we’ve covered the basic syntax, let’s look at some examples now, using fseek()
.
For the entire demonstration, we’ll work with the file sample.txt with the following content:
Hello from JournalDev
This is a sample file
This is the last line.
Using fseek() in C / C++ – Some Examples
In our first example, we’ll use fseek()
, along with fread()
, to read from an existing file.
We’ll move the pointer to the start of the file, and place the offset at a dsitance of 5 positions. offset = 5
#include <stdio.h>
int main() {
// Open the file
FILE* fp = fopen("sample.txt", "r");
// Move the pointer to the start of the file
// And set offset as 5
fseek(fp, 5, SEEK_SET);
char buffer[512];
// Read from the file using fread()
fread(buffer, sizeof(buffer), sizeof(char), fp);
printf("File contains: %s\n", buffer);
// Close the file
fclose(fp);
return 0;
}
Output
File contains: from JournalDev
This is a sample file
This is the last line.
As you can see, it only starts reading from position 5, after the first 5 characters. So we do not see Hello
Now, we’ll move the pointer to the end, using SEEK_END
. We’ll append to the same file, by using fwrite()
at the end!
#include <stdio.h>
int main() {
// Open the file for writing
FILE* fp = fopen("sample.txt", "a");
// Move the pointer to the end of the file
fseek(fp, 0, SEEK_END);
char text[] = "This is some appended text";
// Write to the file using fwrite()
fwrite(text, sizeof(buffer), sizeof(char), fp);
printf("Appended:%s to the file!\n", text);
// Close the file
fclose(fp);
return 0;
}
Output
Hello from JournalDev
This is a sample file
This is the last line.
This is some appended text
Indeed, we were able to append the text successfully to the file!
Conclusion
We learned about using the fseek() function in C / C++, which is quite useful if you want to shift the file pointer.
References
- Linux manual page on the fseek() function in C