Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #23783

Re: fseek

From Les Cargill <lcargill99@comcast.com>
Newsgroups comp.lang.c
Subject Re: fseek
Date 2012-07-26 23:13 -0500
Organization A noiseless patient Spider
Message-ID <jut49v$bgf$1@dont-email.me> (permalink)
References <ab0dc9f6-1432-4b00-b8db-a68d3b14f6a9@googlegroups.com>

Show all headers | View raw


Bill Cunningham wrote:
> This seems like a simple enough question that it shouldn't be too difficult in clc discussion. If the discussion would turn to learning or teaching. I want to allocate enough memory to hold a whole file. malloc is of course going to be needed so I think fseek is the way to learn how big a file is. I tried this and the code compiled find and displayed nothing so I'm at a loss as to what happened. I deleted the source code so I will make references to it.
>
> #include <stdio.h>
>
> FILE *fp;
> if ((fp=fopen("j","rb"))==NULL)
> perror("fopen 1 error");
> long l;

You don't really need the eof check
nor the "while..." Just call fseek().

> while(!feof(fp))
> l=fseek(fp,0,SEEK_SET);

you're close. Try
l=fseek(fp,0,SEEK_END);

That will at least get you to the next bug.

And can I buy you some indentation? :) Good
habit to develop.

> fclose(fp);

Closing a file and then calling eof is at the
very least bad form.

> printf("%d\n%d\n",l,ftell(fp));
> }
>
> This compiles fine and prints nothing. Did the file pointer move? Or is there a problem with printf?
>
> Bill
>

#include <stdio.h>
#include <assert.h>

int main(void)
{
    FILE *fp;
    fp=fopen("j","rb");
    assert(fp);

    long l = fseek(fp,0L,SEEK_END);
    assert(l==0);

    printf("l=%d\nFile size is %d\n",l,ftell(fp));

    fclose(fp);

    return 1;
}


--
Les Cargill

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

fseek Bill Cunningham <billcun@suddenlink.net> - 2012-07-26 19:22 -0700
  Re: fseek cartec69@gmail.com - 2012-07-26 20:46 -0700
    Re: fseek Les Cargill <lcargill99@comcast.com> - 2012-07-26 23:15 -0500
  Re: fseek Les Cargill <lcargill99@comcast.com> - 2012-07-26 23:13 -0500
    Re: fseek James Kuyper <jameskuyper@verizon.net> - 2012-07-27 07:20 -0400
      Re: fseek Les Cargill <lcargill99@comcast.com> - 2012-07-27 12:52 -0500
        Re: fseek Keith Thompson <kst-u@mib.org> - 2012-07-27 16:11 -0700
          Re: fseek Keith Thompson <kst-u@mib.org> - 2012-07-27 16:54 -0700
            Re: fseek Les Cargill <lcargill99@comcast.com> - 2012-07-27 23:10 -0500
              Re: fseek James Kuyper <jameskuyper@verizon.net> - 2012-07-28 11:25 -0400
              Re: fseek Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-07-28 17:02 +0100
                Re: fseek Les Cargill <lcargill99@comcast.com> - 2012-07-28 11:39 -0500
                Re: fseek Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-07-28 17:58 +0100
                Re: fseek Les Cargill <lcargill99@comcast.com> - 2012-07-28 16:07 -0500
                Re: fseek Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-07-28 22:30 +0100
  Re: fseek John Gordon <gordon@panix.com> - 2012-07-27 05:26 +0000
  Re: fseek gordonb.rclzu@burditt.org (Gordon Burditt) - 2012-07-27 12:30 -0500
    Re: fseek Bill Cunningham <billcun@suddenlink.net> - 2012-07-27 15:36 -0700
    Re: fseek Bill Cunningham <billcun@suddenlink.net> - 2012-07-27 15:36 -0700

csiph-web