From a given file (e.g. input.txt) read the alternate nth byte and write it on another file with the help of “lseek”.
lseek (C System Call): lseek is a system call that is used to change the location of the read/write pointer of a file descriptor. The location can be set either in absolute or relative terms.
Function Definition
off_t lseek(int fildes, off_t offset, int whence);
Field Description
int fildes : The file descriptor of the pointer that is going to be moved
off_t offset : The offset of the pointer (measured in bytes).
int whence : The method in which the offset is to be interpreted
(rela, absolute, etc.). Legal value r this variable are provided at the end.
return value : Returns the offset of the pointer (in bytes) from the
beginning of the file. If the return value is -1,
then there was an error moving the pointer.
For example, say our Input file is as follows:

// C program to read nth byte of a file and // copy it to another file using lseek #include <stdio.h> #include <unistd.h> #include <sys/types.h> #include <fcntl.h> void func(char arr[], int n) { // Open the file for READ only. int f_write = open("start.txt", O_RDONLY); // Open the file for WRITE and READ only. int f_read = open("end.txt", O_WRONLY); int count = 0; while (read(f_write, arr, 1)) { // to write the 1st byte of the input file in // the output file if (count < n) { // SEEK_CUR specifies that // the offset provided is relative to the // current file position lseek (f_write, n, SEEK_CUR); write (f_read, arr, 1); count = n; } // After the nth byte (now taking the alternate // nth byte) else { count = (2*n); lseek(f_write, count, SEEK_CUR); write(f_read, arr, 1); } } close(f_write); close(f_read); } // Driver code int main() { char arr[100]; int n; n = 5; // Calling for the function func(arr, n); return 0; } |
This article is contributed by Kishlay Verma. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Read/Write structure to a file in C
- Read/Write Class Objects from/to File in C++
- Input-output system calls in C | Create, Open, Close, Read, Write
- C program to copy contents of one file to another file
- Read a record from a File in C++ using seekg() and tellg()
- Implement your own tail (Read last n lines of a huge file)
- C++ program to read file word by word
- fopen() for an existing file in write mode
- Write a C program that displays contents of a given file like 'more' utility in Linux
- How to write your own header file in C?
- Reverse the content of a file and store it in another
- C++ program to append content of one text file to another
- How to Read and Print an Integer value in C
- How to Read and Print an Integer value in C++
- Write one line functions for strcat() and strcmp()
- Write a program that produces different results in C and C++
- Write your own memcpy() and memmove()
- Write a C program to print "GfG" repeatedly without using loop, recursion and any control structure?
- Write a C program to print "Geeks for Geeks" without using a semicolon
- Write a one line C function to round floating point numbers


