|
From: airam <ai...@so...> - 2003-02-22 15:39:37
|
----- Original Message -----
From: "airam" <ai...@so...>
To: "Per Westermark" <pw...@ia...>
Sent: Saturday, February 22, 2003 5:35 AM
Subject: Re: [Dev-C++] fread
> First of all. If you have a loop to read an integer at a time from a
> file, you better put a write statement WITHIN the loop
> what exactly to write where?sorry i'm not a real programmer)
>
> Second, variable j has room for ONE integer. You try to read countt
> integers into it.
> your comment is right i changed j with *buffer i'm not sure what
buffersize
> is)
>
> Third, you have hardcoded that countt = file size / 4, i.e. assumes that
> the variable j has size 4. While doing the fread(), you suddenly assumes
> that the size of the j integer isn't hardcoded to 4, but is sizeof(x).
> Please inform me what sizeof(x) has do with the size of your input
> buffer.
> countt is how many items i'll read in a file countt=filesize/4 ok i
changed
> sizeof(x) with sizeof(unsigned(int) it's ok?
> i'll build an array for *buffer ok?
>
>
> Fifth. You have a variable 'buffer' that you try to release with a free()
> > call at the end of the program. What were you thinking about using the
> > buffer varable to? Where did you allocate any memory that needs to be
> > released?
> i'll change variable j with *buffer
>
>
> the main idea is (human language)
> get every 4bytes an integer
> see what size is the integer,if size is (between x-y) goto array(k)
> until the end of file
>
>
>
>
> ----- Original Message -----
> From: "Per Westermark" <pw...@ia...>
> To: "airam" <ai...@so...>
> Cc: <dev...@li...>
> Sent: Saturday, February 22, 2003 2:22 PM
> Subject: Re: [Dev-C++] fread
>
>
> > First of all. If you have a loop to read an integer at a time from a
> > file, you better put a write statement WITHIN the loop.
> >
> > Second, variable j has room for ONE integer. You try to read countt
> > integers into it.
> >
> > Third, you have hardcoded that countt = file size / 4, i.e. assumes that
> > the variable j has size 4. While doing the fread(), you suddenly assumes
> > that the size of the j integer isn't hardcoded to 4, but is sizeof(x).
> > Please inform me what sizeof(x) has do with the size of your input
> > buffer.
> >
> > Fourth. Your printout writes out the variable k, while your fread()
tried
> > to put the result into variable j, i.e. you only print out the number of
> > integers - sizeof(x) - read at the last iteration of the loop. If for
some
> > reason feof() or ferror() would return true before entering the loop the
> > first time, your k wouldn't even get initialized.
> >
> > Fifth. You have a variable 'buffer' that you try to release with a
free()
> > call at the end of the program. What were you thinking about using the
> > buffer varable to? Where did you allocate any memory that needs to be
> > released?
> >
> > /Per W
> >
> > On Sat, 22 Feb 2003, airam wrote:
> >
> > > Messagehi!!!!
> > > i want to read a file every 4bytes and get the integer that represent
> 4bytes this is the code (compile successfully)but i don't see nothing!!!!!
> > > whereis is wrong!!!!some help please!!!!
> > > english is not my native language sorry!!!
> > >
> > >
> > > #include <stdio.h>
> > > #include <stdlib.h>
> > >
> > > int main () {
> > > FILE * f;
> > > long size;
> > > int * buffer;
> > > int countt;
> > > int x;
> > > unsigned int j;
> > > unsigned int k;
> > >
> > > f = fopen ( "filename" , "rb" );
> > > if (f==NULL) exit (1);
> > > fseek (f , 0 , SEEK_END);
> > > size = ftell (f);
> > > countt=size/4;
> > > rewind (f);
> > > while (!feof(f) && !ferror(f))
> > > {
> > > k=fread(&j, sizeof(x), countt, f);
> > > }
> > > printf("%d\n",k);
> > > fclose (f);
> > > free (buffer);
> > > return 0;
> > > }
> > >
> > >
> > >
> > >
> >
>
|