fseek in c

If you are seeking about fseek in c then you are at the right blog post.

what is fseek in c?

The fseek in c is used to change the file pointer for the specified stream.

This function is found in the standard input output header file stdio.h.

It seeks in an open file. This function moves the file pointer from its current position to a new position forward or backward specified by a number of bytes.

This function returns zero if successful operation otherwise it returns a non-zero value.

prototype

int fseek(FILE *strm,long int offst,int whnce);

Parameters for fseek

Here strm variable is a pointer to a FILE object that identifies a stream. offst variable is the number of bytes to offset from whence. whnce variable is the position from where offset is added. It is specified by one of the following

  • SEEK_SET : It represents beginning of file.
  • SEEK_CUR : It represents current position of the file pointer.
  • SEEK_END : It represents end of file.

Return value of fseek in c

This function returns zero on successful operation otherwise it returns a non-zero value.


fseek() in c example

Now lets look at fseek in c example program:

#include <stdio.h>
int main()
{
   FILE *fpointer;
   fpointer=fopen("scholar.txt","w");
   fputs("This is text",fpointer);
   fseek(fpointer,8,SEEK_SET);
   fputs("scholarsoul",fpointer);
   fclose(fpointer);
   return 0;
}
fseek  in c example program

Code explanation

fseek function is called and the pointer is set to the position where the text is to be inserted. fputs function is called to insert the text into a file.

fseek() Man page


What is the difference between fseek and fscanf in c?

In c language fseek is used to change the file pointer to the specified stream. Whereas fscanf in c is used to read the data from the file.

Both functions are included in stdio.h header.

The fseek function returns zero in operation is successful otherwise a nonzero value. Whereas fscanf function returns the number of characters that it converted and assigned otherwise EOF on error.

Both functions need a file pointer.

Prototypes are as below

int fseek(FILE *strm,long int offst,int whnce);

int fscanf(FILE *strm,const char *frmt,...);
fseek in c fscanf in c
Used to change the file pointer to the specified stream.Used to take input from the file.
int fseek(FILE *strm,long int offst,int whnce);int fscanf(FILE *strm,const char *frmt,...);
Returns zero if operation is successful otherwise a non-zero value.Returns the number of characters converted and assigned otherwise EOF on error.

What is the difference between fprintf and fseek in c?

In c language fseek is used to change the file pointer to the specified stream. fprintf() sends its formatted data to a specified file.

Both need a file pointer.

Both are included in stdio.h header.

The fseek function returns zero in operation is successful otherwise a nonzero value. Whereas fprintf the function returns a total number of characters written into the file otherwise negative number as an error.

Prototypes are as below

int fseek(FILE *strm,long int offst,int whnce);

int fprintf(FILE *strm,const char *formt,...);
fseek in c fprintf in c
Used to change the file pointer to the specified stream.It sends formatted data to a specified file.
int fseek(FILE *strm,long int offst,int whnce);int fprintf(FILE *strm,const char *formt,…);
Returns zero in operation is successful otherwise a nonzero value.Returns total number of characters written into the file otherwise negative number as error.

What is the difference between ftell and fseek in c?

In c language fseek is used to change the file pointer to the specified stream. The ftell() function is used to get the total size of the file after moving the file pointer at the end of a file.

Both need a file pointer.

Both are included in stdio.h header.

The ftell() the function returns the total size of a file. The fseek function returns zero in operation is successful otherwise a nonzero value.

Prototypes are as below

long int ftell(FILE *strm);

int fseek(FILE *strm,long int offst,int whnce);
fssek in cftell in c
Used to change the file pointer to the specified stream.Used to get the total size of a file after moving pointer to EOF.
int fseek(FILE *strm,long int offst,int whnce);
long int ftell(FILE *strm);
Returns zero in operation is successful otherwise a nonzero value.Returns the current file position of the specified stream wrt the starting of a file.

What is the difference between getc and fseek in c?

The getc() the function gets the next character as unsigned char from the specified stream and advances the pointer. The fseek is used to change the file pointer to the specified stream.

Both functions uses a pointer.

Both are included in stdio.h header file.

The getc() the function returns the character read as an unsigned char cast to an int otherwise EOF on error. The fseek function returns zero in operation is successful otherwise a nonzero value.

Prototypes are as below

int getc(FILE *strm);

int fseek(FILE *strm,long int offst,int whnce);
getc in cfseek in c
Reads the next character as unsigned char from specified stream.Used to change the file pointer to the specified stream.
int getc(FILE *strm);int fseek(FILE *strm,long int offst,int whnce);
Returns the number of characters that it converted and assigned otherwise EOF if error occurs.Returns zero in operation is successful otherwise a nonzero value.

People also ask for

  1. What is the use of fseek in c?

    The fseek is used to change the file pointer to the specified stream.

  2. What are the errors in fseek function if operation fails?

    Here are some errors listed
    1.EAGAIN
    2.EBADF
    3.EFBIG
    4.EINTR
    5.EINVAL
    6.EIO
    7.ENOSPC
    8.ENXIO
    9.EOVERFLOW
    10.EPIPE.

  3. What is the difference between Rewind and fseek in c?

    Rewind function is used to move file pointer position to the beginning of a file. Whereas fseek function is used to change the file pointer for the specified stream.

  4. What is the difference between fseek and fread/fwrite function in c?

    The fseek function is used to change the file pointer for the specified stream. The fread/fwrite functions are used for binary input/output file. 

  5. What is Seek_end?

    SEEK_END represents pointer end of file.

  6. What is the function of Fseek?

    The fseek in c is used to change the file pointer for the specified stream