Code for lseek

E

Elephant

I know what lseek does, but i need an example with simple positioning and
reading the file after positioning for 5 bytes from the beggining(the whole
code) not a line like : lseek(........) and what is it for. If anyone have
some simple code for example I 'd be greatfull
Thanx
 
I

Ico

Elephant said:
I know what lseek does, but i need an example with simple positioning and
reading the file after positioning for 5 bytes from the beggining(the whole
code) not a line like : lseek(........) and what is it for. If anyone have
some simple code for example I 'd be greatfull

I believe lseek() is a POSIX extension, and not a part of standard
ANSI-C. You might want to ask your question at comp.unix.programmers.

By the way, did you even give it a try to find out yourself ?
 
W

Walter Roberson

I know what lseek does, but i need an example with simple positioning and
reading the file after positioning for 5 bytes from the beggining(the whole
code) not a line like : lseek(........) and what is it for. If anyone have
some simple code for example I 'd be greatfull

lseek() is not part of the C standard; you should ask in a Unix/POSIX
newsgroup for lseek() details.

The standard C equivilent is fseek(). Here's an example.


#include <stdio.h>
#include <stdlib.h>
int main(void) {
long int offset = 5L;
int inputchar;
FILE *inputfile = fopen( "filename.dat", "rb" );

if (inputfile == NULL) {
perror( "file open failed" );
exit(EXIT_FAILURE);
}

if ( fseek( inputfile, offset, SEEK_SET ) != 0 ) {
perror( "fseek failed" );
exit(EXIT_FAILURE);
}

inputchar = fgetc( inputfile );
if ( inputchar == EOF ) {
fprintf( stderr, "End of file while reading at offset %ld\n", offset );
exit(EXIT_FAILURE);
}

printf( "The character at location %ld had value 0x%X\n",
offset, (unsigned int) inputchar );

return 0;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top