how to increment pointer in a file

Discussion in 'C Programming' started by pooja, Feb 8, 2004.

  1. pooja

    pooja Guest

    Hi
    I m pooja.I m working on a prj in C.
    I m unable to increment a char pointer in a file, it gives the same
    address as that of the first char.Eg if # is on loc 3000 then i,n ,c
    ,l,u,d,e are on same address.Can any one plz help .
    pooja, Feb 8, 2004
    #1
    1. Advertising

  2. * Thus spoke pooja <>:

    Hallo,

    > I m unable to increment a char pointer in a file, it gives the same
    > address as that of the first char.Eg if # is on loc 3000 then i,n ,c
    > ,l,u,d,e are on same address.Can any one plz help .


    Show us your code.


    Wolfgang.
    --
    "I can remember the exact instant when I realized that a large part of my life
    from then on was going to be spent in finding mistakes in my own programs."
    -- Maurice Wilkes, 1947
    Wolfgang Kaufmann, Feb 8, 2004
    #2
    1. Advertising

  3. pooja wrote:

    > Hi
    > I m pooja.I m working on a prj in C.
    > I m unable to increment a char pointer in a file, it gives the same
    > address as that of the first char.Eg if # is on loc 3000 then i,n ,c
    > ,l,u,d,e are on same address.Can any one plz help .


    You sound like a Pascal programmer. Forget anything you learned about
    pointers to i/o streams.

    With an input file "foo" with one line
    #include
    run this and see what happens:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>

    /* The following function need not do anything useful. fpos_t is not
    necessarily an arithmetic type. */
    void showfpos(fpos_t * x)
    {
    if (sizeof(fpos_t) == sizeof(char)) {
    unsigned char y;
    memmove(&y, x, sizeof(char));
    printf("fpos_t %u:", y);
    return;
    }
    if (sizeof(fpos_t) == sizeof(int)) {
    unsigned int y;
    memmove(&y, x, sizeof(int));
    printf("fpos_t %u:", y);
    return;
    }
    if (sizeof(fpos_t) == sizeof(long)) {
    unsigned long y;
    memmove(&y, x, sizeof(long));
    printf("fpos_t %lu:", y);
    return;
    }
    if (sizeof(fpos_t) == sizeof(long long)) {
    unsigned long long y;
    memmove(&y, x, sizeof(long long));
    printf("fpos_t %llu:", y);
    return;
    }
    printf("fpos_t ?:");
    }

    int main(void)
    {
    FILE *f;
    size_t nread;
    char c;
    fpos_t loc;
    printf("Trying with ftell\n");
    if (!(f = fopen("./foo", "rb"))) {
    fprintf(stderr, "could not open 'foo' for reading\n");
    exit(EXIT_FAILURE);
    }
    printf("ftell %ld after open\n", ftell(f));
    while (fread(&c, 1, 1, f))
    printf("ftell %ld after getting %c\n", ftell(f), c);
    printf("ftell %ld after fread failed\n", ftell(f));
    fclose(f);


    printf("Trying with fgetpos\n");
    if (!(f = fopen("./foo", "rb"))) {
    fprintf(stderr, "could not open 'foo' for reading\n");
    exit(EXIT_FAILURE);
    }
    fgetpos(f, &loc);
    showfpos(&loc);
    printf("after open\n");
    while (fread(&c, 1, 1, f)) {
    fgetpos(f, &loc);
    showfpos(&loc);
    printf(" after getting %c\n", c);
    }
    fgetpos(f, &loc);
    showfpos(&loc);
    printf(" after fread failed\n", ftell(f));
    fclose(f);
    return 0;
    }

    Trying with ftell
    ftell 0 after open
    ftell 1 after getting #
    ftell 2 after getting i
    ftell 3 after getting n
    ftell 4 after getting c
    ftell 5 after getting l
    ftell 6 after getting u
    ftell 7 after getting d
    ftell 8 after getting e
    ftell 9 after getting

    ftell 10 after getting

    ftell 10 after fread failed
    Trying with fgetpos
    fpos_t 0:after open
    fpos_t 1: after getting #
    fpos_t 2: after getting i
    fpos_t 3: after getting n
    fpos_t 4: after getting c
    fpos_t 5: after getting l
    fpos_t 6: after getting u
    fpos_t 7: after getting d
    fpos_t 8: after getting e
    fpos_t 9: after getting

    fpos_t 10: after getting

    fpos_t 10: after fread failed


    --
    Martin Ambuhl
    Martin Ambuhl, Feb 8, 2004
    #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. deepak

    Increment file pointer

    deepak, Oct 15, 2003, in forum: C Programming
    Replies:
    21
    Views:
    2,167
    Barry Schwarz
    Oct 18, 2003
  2. Replies:
    104
    Views:
    10,997
    Jordan Abel
    Oct 28, 2005
  3. Replies:
    99
    Views:
    2,498
    eliza81
    Jun 11, 2010
  4. Alf P. Steinbach /Usenet
    Replies:
    0
    Views:
    890
    Alf P. Steinbach /Usenet
    May 22, 2011
  5. Peng Yu

    post increment or pre increment?

    Peng Yu, Nov 21, 2008, in forum: Perl Misc
    Replies:
    7
    Views:
    518
    Peter J. Holzer
    Nov 23, 2008
Loading...

Share This Page