Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

fseek() in C

Introduction to fseek() in C

As we all know that C is one of the commonly used programming languages. It also deals with working in the files and performing all the operations related to the files. C provides inbuilt library functions that are ready to use by importing the library in the program and following their syntax accordingly. fseek() Function is one of the C standard library functions which belongs to the stdio.h library. fseek() in C is used to change the file pointer (in order to point to a specific file position) to the desired location. Once the file pointer is moved to a specified offset, we can perform any operation like writing, reading from the file according to the programmer requirements.

Syntax

The basic syntax of how the fseek() function is used in C is given below:

int fseek(FILE *stream, long int offset, int pos)

Where,

  • stream: It indicates the file object that is used to identify the file stream.
  • offset: It is defined in ‘long integer’ data type and is used to specify the offset in terms of the number of bytes or characters where the position indicator needs to be placed to define the new file position.
  • pos: It defines the point where the file offset needs to be added. In other terms, it defines the position where the file pointer needs to be moved.

It is generally defined by 3 constants given below:

  • SEEK_CUR:  It indicates the current position of the pointer of the file.
  • SEEK_END: As the name indicates, it moves the pointer of the file to the end of the file.
  • SEEK_SET: As the name indicates, it moves the file pointer to the beginning of the start of the file

How does fseek() function work in C?

As explained above in the syntax of fseek() function, it takes 3 arguments, the first one being the file pointer, the second one being the number of bytes/ characters to be moved and the third defines the position where the file pointer to be moved. So fseek() is used with its all 3 arguments along with the other file functions to perform their respective tasks.  It is basically a C function used for handling the file operations. In C, file pointer is returned by the fopen() function (which is also an in-built function)  used for opening any file in C and ‘numbytes’ is used to return the number of bytes from the file origin. By using the fseek() function in C, the desired read and write operations can be performed by locating the file pointer and moving it accordingly.

Example to Implement fseek() in C

Below given are some of the examples mentioned :

Example #1

Using the fseek() function in C in the read mode of the file.

Code:

#include
int main()
{
FILE *fx;
fx = fopen("new_file.txt", "r");
//Using the fseek function to move the file pointer to the end
fseek(fx, 0, SEEK_END);
// Using the file function in order to Print the position of file pointer
printf("Position of file pointer is : ");
printf("%ld \n", ftell(fx));
// Using the file function 'fseek' to move the file position 10 characters ahead
fseek(fx,10,SEEK_SET);
int ch;
// printing the resulting file after 10 characters
printf("Resulting bytes after the 10 characters in a file are: ");
while( (ch=fgetc(fx)) != EOF)
// using function 'putchar(x)'' to print the file characters on console
putchar(ch);
return 0;
}

File: new_file.txt:

Hello we are learning fseek function in c

Output:

Explanation: In the above code, there is a file ‘new_file.txt’ that already exists in the system. First, we are including the stdio.h library which is mandatory to use fseek() function in C for any file related I/O operations. First, the file is opened using the fopen() function and the file pointer is returned by it. Now the fseek() function is used to move the file pointer to the end of the file. After that the total number of file characters are printed on the console or the position of the pointer which is at the last of the file is printed using the ‘ftell()’ function. fseek() function is again used in order to move the file pointer to the 11th position and then the resulting characters of file after skipping the first 10 characters are printed on the console using putchar() function.

Example #2

Using the fseek() function in C in write mode.

 Code:

// C library mandatory to use C I/O operation
#include
int main () {
// defining the file pointer in order to perform file operations
FILE *fx;
// Opening a file using the 'fopen()' function in write mode
fx = fopen("file_new.txt","w+");
// writing in the file using 'fputs() function'
fputs("Yes, we are learning fseek function in C", fx);
//using fseek() function to move the file pointer after 12 characters
fseek( fx, 12, SEEK_SET );
// inserting the data in the file
fputs("trying to insert something in between", fx);
//closing the file using 'fclose() function'
fclose(fx);
return(0);
}

Output File: file_new.txt is created in the system with the following data in it.

Explanation: In the above example, we are performing the simple task of creating a new file with the name ‘file_new.txt’. File is opened using the function ‘fopen()’ in the write mode ( that’s why we have used w+). In order to isert the data in the file, fputs() function is used with the string which needs to be inserted. Now the fseek() function is used in order to move the file pointer 12 characters ahead in the file. fputs() function is again used in order to overwrite the data of the file. After performing all the operations, fclose() function is used to close the file.

Conclusion

The above description clearly explains the fseek() function in C and how it is used in C language. If the fseek() operation is successful, it returns a zero value otherwise it would return a non-zero value. As working with the files is an important aspect of C language, it also offers various inbuilt functions like fopen(), fclose(), getw(), putw().Before using any function,  it is very important to have in depth knowledge in advance so that it can be used appropriately.

Recommended Articles

This is a guide to fseek() in C. Here we discuss an introduction to fseek() in C, syntax, parameters, how does it work with programming examples. You can also go through our other related articles to learn more –

  1. Unary Operator In C
  2. Multidimensional Array In C
  3. Regular Expression In C
  4. Reverse String In C

The post fseek() in C appeared first on EDUCBA.



This post first appeared on Free Online CFA Calculator Training Course | EduCB, please read the originial post: here

Share the post

fseek() in C

×

Subscribe to Free Online Cfa Calculator Training Course | Educb

Get updates delivered right to your inbox!

Thank you for your subscription

×