|
From: Per W. <pw...@ia...> - 2003-02-22 12:24:01
|
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;
> }
>
>
>
>
|