|
From: airam <ai...@so...> - 2003-02-21 22:16:39
|
Hi!!!!!!
i want to read a file every 4bytes
and after printf
this is the code but doesn't work!!
#include <stdio.h>
int main()
{
unsigned int i,j,k;
FILE *f;
f=3Dfopen("filename","r");
if (!f)
return 1;
fseek(f,0,SEEK_SET);
for (i=3D0;i<sizeof file; i++)
{
fread(&j,sizeof(unsigned int),2,f);
fseek(f,sizeof(unsigned int),SEEK_CUR);
}
fclose(f);
printf("\n");
}
|
|
From: P.D. v. Z. <pd...@xs...> - 2003-02-21 22:36:14
|
"sizeof file" does not do what you expect it to do. "sizeof" returns the
size of a type or an object, so in your case it returns the size of the
"FILE" struct, not the size of the file you opened.
To correctly loop through the file, you'd use something like:
while (!feof(f) && !ferror(f))
{
fread(&j, sizeof(unsigned int), 2, f);
// the fseek() is not necessary
// if you want to display what you've read from the file, I'd use a
printf() call or similar here
}
-----Original Message-----
From: dev...@li...
[mailto:dev...@li...] On Behalf Of airam
Sent: Friday, February 21, 2003 11:16 AM
To: dev...@li...
Subject: [Dev-C++] fread
Hi!!!!!!
i want to read a file every 4bytes
and after printf
this is the code but doesn't work!!
#include <stdio.h>
int main()
{
unsigned int i,j,k;
FILE *f;
f=fopen("filename","r");
if (!f)
return 1;
fseek(f,0,SEEK_SET);
for (i=0;i<sizeof file; i++)
{
fread(&j,sizeof(unsigned int),2,f);
fseek(f,sizeof(unsigned int),SEEK_CUR);
}
fclose(f);
printf("\n");
}
|
|
From: airam <ai...@so...> - 2003-02-22 10:49:35
|
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>
=20
int main () {
FILE * f;
long size;
int * buffer;
int countt;
int x;
unsigned int j;
unsigned int k;
=20
f =3D fopen ( "filename" , "rb" );
if (f=3D=3DNULL) exit (1);
fseek (f , 0 , SEEK_END);
size =3D ftell (f);
countt=3Dsize/4;
rewind (f);
while (!feof(f) && !ferror(f))
{
k=3Dfread(&j, sizeof(x), countt, f);
}
printf("%d\n",k);
fclose (f);
free (buffer);
return 0;
}
=20
|
|
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;
> }
>
>
>
>
|
|
From: Ioannis V. <no...@ho...> - 2003-02-22 05:34:04
|
-------Original Message-------=0D
=0D
From: airam=0D
Date: =D3=DC=E2=E2=E1=F4=EF, 22 =D6=E5=E2=F1=EF=F5=DC=F1=E9=EF=F2 2003 12=
:19:18 =F0=EC=0D
To: dev...@li...=0D
Subject: [Dev-C++] fread=0D
=0D
Hi!!!!!!=0D
i want to read a file every 4bytes=0D
and after printf=0D
this is the code but doesn't work!!=0D
=0D
The above "every 4 bytes" of yours is not clear. I assume you mean "read =
to
an unsigned int leaving 4 bytes space between two reads in text mode=0D
=0D
=0D
=0D
#include <stdio.h>=0D
=0D
/* For EXIT_FAILURE */=0D
#include <stdlib.h>=0D
=0D
int main()=0D
{=0D
unsigned int j;=0D
size_t input;=0D
=0D
=0D
FILE *f;=0D
=0D
f=3Dfopen("filename","r");=0D
=0D
if (!f)=0D
return EXIT_FAILURE;=0D
=0D
/* or do-while(1) */=0D
while(1)=0D
{=0D
input=3Dfread(&j,sizeof(unsigned int),2,f);=0D
=0D
if(input<2)=0D
{=0D
fclose(f);=0D
return 0;=0D
}=0D
=0D
printf("%u\n", j);=0D
=0D
fseek(f,4,SEEK_CUR);=0D
=0D
}=0D
=0D
/* Required in C90 */=0D
return 0;=0D
=0D
}=0D
=20 |
|
From: Per W. <pw...@ia...> - 2003-02-22 10:19:26
|
See below - slight fix added.
/Per W
On Sat, 22 Feb 2003, Ioannis Vranos wrote:
> -------Original Message-------
>
> From: airam
> Date: =D3=DC=E2=E2=E1=F4=EF, 22 =D6=E5=E2=F1=EF=F5=DC=F1=E9=EF=F2 2003 12=
:19:18 =F0=EC
> To: dev...@li...
> Subject: [Dev-C++] fread
>
> Hi!!!!!!
> i want to read a file every 4bytes
> and after printf
> this is the code but doesn't work!!
>
> The above "every 4 bytes" of yours is not clear. I assume you mean "read =
to
> an unsigned int leaving 4 bytes space between two reads in text mode
>
>
>
> #include <stdio.h>
>
> /* For EXIT_FAILURE */
> #include <stdlib.h>
>
> int main()
> {
//> unsigned int j;
unisgned j[2];
> size_t input;
>
>
> FILE *f;
>
> f=3Dfopen("filename","r");
>
> if (!f)
> return EXIT_FAILURE;
>
> /* or do-while(1) */
> while(1)
> {
//> input=3Dfread(&j,sizeof(unsigned int),2,f);
// Need room for two integers, if request is fo two integers.
input=3Dfread(j,*j,2,f);
>
> if(input<2)
> {
> fclose(f);
> return 0;
> }
>
//> printf("%u\n", j);
printf("%u\n",j[0]);
>
// fseek not needed - already eaten away the 'extra' integer.
//> fseek(f,4,SEEK_CUR);
Small seeks forward as in this case - like Iannis I'm assuming the request
was to read every other number, i.e. to read one, skip one, read one...
- should normally be ignored, and the skip be performed by a read to a
temporary buffer until the new target is found. For a number of streams -
redirected input, an encrypted file, ... the stream must anyway perform
the read of the 'skipped' information. Some implementations, especially
encrypted or compressed streams, might support the seek operation, but
instead take a quite expensive synchronization to find the new location to
read from.
Seek backward is even more problematic. A large number of stream
constructs doesn't support a backward seek at all, so it's a good idea
when implementing an algorithm to see if backward seek may be avoided.
>
> }
>
> /* Required in C90 */
> return 0;
>
> }
>
|