On Sat, 16 Jul 2005 18:35:39 GMT, cs <> wrote:
>i don't know if this compile but
>the question is why this program has the result down?
i forgot "b"== binary mode
#include <stdio.h>
#include <stdlib.h>
typedef struct{
unsigned a;
unsigned *b;
}tp;
int main(void)
{ int i;
unsigned val[90]={0};
tp e;
size_t k;
FILE *fp;
/**********************/
e.a=90; e.b=val;
for(i=0; i<e.a; ++i) e.b[i]=rand();
if( (fp=fopen("file.dat", "wb")) == 0 ) return 0;
k=fwrite(&e.a, sizeof(unsigned), 1, fp);
if(k<1) {fclose(fp); return 0;}
k=fwrite(e.b, sizeof(unsigned), e.a, fp);
if(k<e.a) {fclose(fp); return 0;}
if( fclose(fp)==EOF ) printf("W Error\n");
for(i=0; i<e.a; ++i)
{printf("e.b[%d]=%x ", i, e.b[i]); e.b[i]=0; }
printf("\n");
if( (fp=fopen("file.dat", "rb")) == 0 ) {printf("fopen"); goto la;}
k=fread(&e.a, sizeof(unsigned), 1, fp);
if(e.a>20000 || k<1 || ferror(fp))
{fclose(fp); return 0;}
k=fread(e.b, sizeof(unsigned), e.a, fp);
if(k<e.a || ferror(fp))
{fclose(fp);
printf("E0 k=%u e.a=%u\n", (unsigned) k, e.a);
return 0;
}
if( fclose(fp)==EOF )
{la:;
printf("R Error\n");
}
for(i=0; i<e.a; ++i)
{printf("e.b[%d]=%x ", i, e.b[i]); e.b[i]=0; }
printf("\n");
return 0;
}