#include #include #define MY_BSIZE 10000 int main(void) { FILE * file=fopen("ble.txt", "w+b"); unsigned char buff[MY_BSIZE], ibuff[MY_BSIZE]; long i, cnt; int c; printf("Testing binary input-output\n"); /* fill buffer */ for(i=0;i255) { printf("Char value out of range%s\n", (c==EOF)?", maybe unexpected EOF":""); break; } ibuff[i]=c; } if(memcmp(buff, ibuff, MY_BSIZE)) printf("Read different values then written\n"); /* Read all file at once as one record */ printf("Test 2\n"); fseek(file, 0L, SEEK_SET); cnt=fread(ibuff, MY_BSIZE, 1, file); if(cnt!=1) printf("Short read\n"); if(memcmp(buff, ibuff, MY_BSIZE)) printf("Read different values then written\n"); /* Read all file at once as one byte records */ printf("Test 3\n"); fseek(file, 0L, SEEK_SET); cnt=fread(ibuff, 1, MY_BSIZE, file); if(cnt!=MY_BSIZE) printf("Short read\n"); if(memcmp(buff, ibuff, MY_BSIZE)) printf("Read different values then written\n"); /* fread - small blocks proportional to buffer */ printf("Test 4\n"); fseek(file, 0L, SEEK_SET); for(i=0;i0?100:100+i; fseek(file, i>0? i:0, SEEK_SET); cnt=fread(ibuff+i, cnt, 1, file); if(cnt!=1) { printf("Short read\n"); break; } } while (i>0); if(memcmp(buff, ibuff, MY_BSIZE)) printf("Read different values then written\n"); /* Reading backwards, mixing getc and fread */ printf("Test 7\n"); i=MY_BSIZE; c=0; do { i-=100; cnt=i>0?100:100+i; fseek(file, i>0? i:0, SEEK_SET); c=1-c; if(c) { cnt=fread(ibuff+i, cnt, 1, file); if(cnt!=1) { printf("Short read\n"); break; } } else { int j; for(j=0;j<100;j++) { int cc=getc(file); if(cc==EOF) { printf("Unexpected EOF\n"); goto fail2; } ibuff[i+j]=cc; } } } while (i>0); if(memcmp(buff, ibuff, MY_BSIZE)) printf("Read different values then written\n"); fail2: /* Using fgetpos and fsetpos */ printf("Test 8\n"); { fpos_t pos; long off; fseek(file, 0L, SEEK_SET); for(i=0;i<200;i++) { int cc = getc(file); if(cc==EOF) { printf("Unexpected EOF\n"); goto fail3; } ibuff[i]=cc; } fgetpos(file, &pos); /* off = ftell(file); */ fseek(file, 512, SEEK_SET); cnt=fread(ibuff+512,MY_BSIZE-512,1,file); if(cnt!=1) { printf("Short read\n"); goto fail3; } fsetpos(file, &pos); /* fseek(file, off, SEEK_SET); */ cnt=fread(ibuff+200,312,1,file); if(cnt!=1) { printf("Short read\n"); goto fail3; } } if(memcmp(buff, ibuff, MY_BSIZE)) printf("Read different values then written\n"); fail3: /* Writing backwards, mixing fwrite and fputc */ printf("Test 9\n"); i=MY_BSIZE; c=0; do { i-=100; cnt=i>0?100:100+i; fseek(file, i>0? i:0, SEEK_SET); c=1-c; if(c) { cnt=fwrite(buff+i, cnt, 1, file); if(cnt!=1) { printf("Short write\n"); break; } } else { int j; for(j=0;j<100;j++) { putc(buff[i+j], file); } } } while (i>0); fseek(file, 0L, SEEK_SET); cnt=fread(ibuff, MY_BSIZE, 1, file); if(cnt!=1) { printf("Short read\n"); } if(memcmp(buff, ibuff, MY_BSIZE)) printf("Read different values then written\n"); /* Test of EOF conditions (feof, getc) */ printf("Test 10\n"); fseek(file, 0L, SEEK_SET); if(feof(file)) { printf("Unexpected EOF (1)\n"); } fread(ibuff,MY_BSIZE,1,file); if(feof(file)) { printf("Unexpected EOF (2)\n"); } getc(file); if(!feof(file)) { printf("Expected EOF, feof disagrees\n"); } fseek(file, 0L, SEEK_SET); if(feof(file)) { printf("Unexpected EOF (3)\n"); } for(i=0;i