Here are the answers:
1. To print 10 characters starting from the 10th character, you need to use lseek() to move the file pointer to the 10th position and then read 10 characters:
int fd = open("seeking", O_RDONLY);
lseek(fd, 10, SEEK_SET);
read(fd, buff, 10);
write(1, buff, 10);
close(fd);
2. To find the size of a file using lseek(), you can use lseek() to move the file pointer to the end and check its position which will give the size of the file:
int fd = open("file", O_RDONLY);
off
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
93 views7 pages
Tutorial-4 (Lseek)
Here are the answers:
1. To print 10 characters starting from the 10th character, you need to use lseek() to move the file pointer to the 10th position and then read 10 characters:
int fd = open("seeking", O_RDONLY);
lseek(fd, 10, SEEK_SET);
read(fd, buff, 10);
write(1, buff, 10);
close(fd);
2. To find the size of a file using lseek(), you can use lseek() to move the file pointer to the end and check its position which will give the size of the file:
int fd = open("file", O_RDONLY);
off
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7
lseek() system call
• lseek() system call repositions the read/write file offset i.e., it
changes the positions of the read/write pointer within the file. • In every file any read or write operations happen at the position pointed to by the pointer. • lseek() system call helps us to manage the position of this pointer within a file. • e.g., let’s suppose the content of a file F1 is “1234567890” but you want the content to be “12345hello”. You simply can’t open the file and write “hello” because if you do so then “hello” will be written in the very beginning of the file. This means you need to reposition the pointer after ‘5’ and then start writing “hello”. • lseek() will help to reposition the pointer and write() will be used to write “hello” lseek() system call #include <sys/types.h> #include <unistd.h> off_t lseek(int fd, off_t offset, int whence);
The first parameter is the file descriptor of the file,
which you can get using open() system call. The second parameter specifies how much you want the pointer to move and the third parameter is the reference point of the movement i.e., beginning of file(SEEK_SET), current position(SEEK_CUR) of pointer or end of file(SEEK_END). lseek() system call • Examples: – lseek(fd,5,SEEK_SET) – this moves the pointer 5 positions ahead starting from the beginning of the file – lseek(fd,5,SEEK_CUR) – this moves the pointer 5 positions ahead from the current position in the file – lseek(fd,-5,SEEK_CUR) – this moves the pointer 5 positions back from the current position in the file – lseek(fd,-5,SEEK_END) -> this moves the pointer 5 positions back from the end of the file lseek() system call • Examples: – lseek(fd,5,SEEK_SET) – this moves the pointer 5 positions ahead starting from the beginning of the file – lseek(fd,5,SEEK_CUR) – this moves the pointer 5 positions ahead from the current position in the file – lseek(fd,-5,SEEK_CUR) – this moves the pointer 5 positions back from the current position in the file – lseek(fd,-5,SEEK_END) -> this moves the pointer 5 positions back from the end of the file • On success, lseek() returns the position of the pointer within the file as measured in bytes from the beginning of the file. But, on failure, it returns -1. lseek() system call • Create a file “seeking” #include<unistd.h> and write #include<fcntl.h>
“1234567890abcdefghij #include<sys/types.h
xxxxxxxxxx” into it.
#include<sys/stat.h> int main() { • We want to write a int n,f; program that reads 10 char buff[10]; characters from file f=open("seeking",O_RDWR); “seeking” and print on read(f,buff,10); write(1,buff,10); read(f,buff,10); screen. Again read 10 write(1,buff,10); characters and write on } screen. Output: ??? lseek() system call Program using lseek() system call that reads 10 characters from file “seeking” and print on screen. Skip next 5 characters and again read 10 characters and write on screen. lseek() system call Q.1 Write a program to print 10 characters starting from the 10th character from a file “seeking”.
Q.2 How can you find the size of the file using lseek() system call?