The use of Lseek () function and fseek () function in C language _c language

Source: Internet
Author: User
Tags error code

C-language Lseek () function: Moving a file's read/write location

Header file:

 #include <sys/types.h>  #include <unistd.h>

To define a function:

off_t lseek (int fildes, off_t offset, int whence);

Function Description:
Each open file has a read and write location, and when the file is opened it usually points to the beginning of the file, and if you open the file in an additional way (such as O_append), the read-write location points to the end of the file. When read () or write (), the read-write location increases, and Lseek () is used to control the read-write location of the file. Parameter Fildes is an open file descriptor, and the parameter offset is the number of displacements that move the read-write position based on the parameter whence.

The parameter whence is one of the following:

    • Seek_set parameter offset is the new read-write location.
    • The seek_cur increases the offset by the current reading and writing position.
    • Seek_end the read and write position to the end of the file and then increases the offset amount. When the whence value is seek_cur or
    • When Seek_end, the parameter Offet allows negative values to appear.

The following are special ways to use the teaching:
1 to move the read-write position to the beginning of the file: Lseek (int fildes, 0, Seek_set);
2 to move the read-write position to the end of the file: Lseek (int fildes, 0, seek_end);
3 to obtain the current file location: lseek (int fildes, 0, seek_cur);

Return value: Returns the current read-write location, which is the number of bytes from the beginning of the file, when the call succeeds. If there is an error return-1, errno will hold the error code.

Additional instructions: The Linux system does not allow Lseek () to the TTY device, this action will make Lseek () return to Espipe.

C language Fseek () function: To move file stream to read and write location
header file:

#include <stdio.h>

To define a function:

int fseek (FILE * stream, long offset, int whence);

Function Description:
Fseek () is used to move the read-write location of the file stream.

1, the parameter stream is an open file pointer,
2, the parameter offset is based on the parameter whence to move the reading and writing position displacement number. The parameter whence is one of the following:
Seek_set the offset displacement from the beginning of the file to the new read-write location. The seek_cur increases the offset by the current reading and writing position.
Seek_end the read and write position to the end of the file and then increases the offset amount. When the whence value is seek_cur or
When seek_end, parameter offset allows negative values to appear.

The following are more specific ways to use:
1 to move the read-write location to the beginning of the file: fseek (file *stream, 0, Seek_set);
2 to move the read-write location to the end of the file: fseek (file *stream, 0, 0seek_end);

Return value: Returns 0 when the call succeeds, returns 1 if there is an error, and errno the error code.

Additional note: fseek () does not return a read-write location like Lseek (), so you must use Ftell () to get the current read-write location.

Example

#include <stdio.h>
Main ()
{
  FILE * stream;
  Long offset;
  fpos_t Pos;
  stream = fopen ("/etc/passwd", "R");
  Fseek (Stream, 5, seek_set);
  printf ("offset =%d\n", Ftell (Stream));
  Rewind (stream);
  Fgetpos (stream, &pos);
  printf ("offset =%d\n", POS);
  pos = ten;
  Fsetpos (stream, &pos);
  printf ("offset =%d\n", Ftell (Stream));
  Fclose (stream);
}

Perform

offset = 5
offset = 0
offset = 10

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.