|
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;
>
> }
>
|