lseek() sample program in C

By: Baski Emailed: 1757 times Printed: 2444 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

Input and output are normally sequential: each read or write takes place at a position in the file right after the previous one. When necessary, however, a file can be read or written in any arbitrary order. The system call lseek provides a way to move around in a file without reading or writing any data:
   long lseek(int fd, long offset, int origin);
sets the current position in the file whose descriptor is fd to offset, which is taken relative to the location specified by origin. Subsequent reading or writing will begin at that position. origin can be 0, 1, or 2 to specify that offset is to be measured from the beginning, from the current position, or from the end of the file respectively. For example, to append to a file (the redirection >> in the UNIX shell, or "a" for fopen), seek to the end before writing:
   lseek(fd, 0L, 2);
To get back to the beginning (``rewind''),
   lseek(fd, 0L, 0);
Notice the 0L argument; it could also be written as (long) 0 or just as 0 if lseek is properly declared.

With lseek, it is possible to treat files more or less like arrays, at the price of slower access. For example, the following function reads any number of bytes from any arbitrary place in a file. It returns the number read, or -1 on error.

   #include "syscalls.h"

   /*get:  read n bytes from position pos */
   int get(int fd, long pos, char *buf, int n)
   {
       if (lseek(fd, pos, 0) >= 0) /* get to pos */
           return read(fd, buf, n);
       else
           return -1;
   }
The return value from lseek is a long that gives the new position in the file, or -1 if an error occurs. The standard library function fseek is similar to lseek except that the first argument is a FILE * and the return is non-zero if an error occurred.

C Home | All C Tutorials | Latest C Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(4)


1. View Comment

To determine current location in a file:
{
if (lseek(STDIN_FILENO, 0, SEEK_CUR) == -1)
printf ("cannot seek\n");
else
printf("seek at %d", &SEEK_CUR);
exit(0);
}


View Tutorial          By: Terry at 2009-09-27 13:15:56
2. View Comment

i like google

View Tutorial          By: parikh dhruti at 2013-02-12 06:43:19
3. View Comment

i don't know anything about computer..........

View Tutorial          By: Ami weds Pappu....... at 2013-02-14 04:00:11
4. View Comment

Thank you, that was short and enough :)

View Tutorial          By: Bilal korir at 2014-05-20 21:37:12

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Baski
Compiling multiple source files and specifying classpath using javac
The equals() Method example in Java
Default Values for Data Types in Java
JSP Example to connect to MS SQL database using Tomcat Connection Pool
<convertNumber> and <convertDateTime> in JSF
Enable/Disable Scripting Elements in JSP
Using calloc() Function in C
lseek() sample program in C
Arrays of Structures example program in C
The Basic Syntax Expression Language in JSP
Initialization of Pointer Arrays in C
Functions returning non-integer values in C
Increment and Decrement Operators in C
Getting Started with C
What is JSF (JavaServer Faces)?

More Tutorials in C
Sum of the elements of an array in C
Printing a simple histogram in C
Sorting an integer array in C
Find square and square root for a given number in C
Simple arithmetic calculations in C
Command-line arguments in C
Calculator in C
Passing double value to a function in C
Passing pointer to a function in C
Infix to Prefix And Postfix in C
while, do while and for loops in C
Unicode and UTF-8 in C
Formatting with printf in C
if, if...else and switch statements in C with samples
Statements in C

More Latest News
Most Viewed Articles (in C )
Using memset(), memcpy(), and memmove() in C
Open, Creat, Close, Unlink system calls sample program in C
UNIX read and write system calls sample program in C
Find square and square root for a given number in C
lseek() sample program in C
assert() Function Example program in C
Character Arrays in C
Using free() Function in C
Printing a simple histogram in C
perror() Function - example program in C
Infix to Prefix And Postfix in C
scanf and sscanf sample program in C
Macro Substitution using #define in C
goto and labels in C
Fopen and Getc implementation program in C
Most Emailed Articles (in C)
Type Conversions in C (String to Integer, isdigit() etc)
Pointers and Arrays in C
Using malloc() Function in C
Using realloc() Function in C
The Birth and history of C Programming Language
Basics of C
Getting Started with C
Variables and Arithmetic Expressions in C
The for statement in C
Symbolic Constants using #define in C
File Copying in C
Character Counting sample program in C
Line Counting sample program in C
Word Counting sample program in C
Arrays sample program in C