Code for lseek

Discussion in 'C Programming' started by Elephant, Jan 12, 2006.

  1. Elephant

    Elephant Guest

    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
    Elephant, Jan 12, 2006
    #1
    1. Advertising

  2. Elephant

    Ico Guest

    Elephant <> wrote:
    > 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 ?

    --
    :wq
    ^X^Cy^K^X^C^C^C^C
    Ico, Jan 12, 2006
    #2
    1. Advertising

  3. In article <dq65mg$m5k$-com.hr>, Elephant <> wrote:
    >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;
    }
    --
    Okay, buzzwords only. Two syllables, tops. -- Laurie Anderson
    Walter Roberson, Jan 12, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. venkat

    regarding lseek and fread

    venkat, May 27, 2007, in forum: C Programming
    Replies:
    2
    Views:
    432
    Martin Ambuhl
    May 27, 2007
  2. golden

    lseek and write question

    golden, Nov 16, 2007, in forum: C++
    Replies:
    3
    Views:
    574
    James Kanze
    Nov 17, 2007
  3. Gordon Beaton
    Replies:
    3
    Views:
    1,280
  4. pavunkumar

    Lseek Error

    pavunkumar, Mar 31, 2009, in forum: C Programming
    Replies:
    2
    Views:
    457
    Rob Clarke
    Mar 31, 2009
  5. Replies:
    3
    Views:
    117
    Andreas Perstinger
    May 14, 2013
Loading...

Share This Page