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:
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