[comp.sys.atari.st] Megamax Binary Files

nowlin@ihuxy.UUCP (05/14/87)

I've noticed a problem with Megamax and reading binary files.  The
following code is used to copy one file to another.  It works fine except
that the file being copied, the one being read, gets one byte longer after
every copy.

	if ((in = fopen(old,"br")) == NULL) {
		fprintf(stderr,"ERROR: can't open '%s' to read\n",old);
		return 0;
	}

	if ((out = fopen(new,"bw")) == NULL) {
		fprintf(stderr,"ERROR: can't open '%s' to write\n",new);
		fclose(in);
		return 0;
	}

	setbuf(in,ibuf);
	setbuf(out,obuf);

	while ((len = fread(buff,1,CPBLEN,in)) > 0) {
		if (fwrite(buff,1,len,out) != len) {
			fprintf(stderr,"ERROR: write error on '%s'\n",new);
			ret = 0;
			break;
		}
	}

	if (fclose(in) < 0) {
		fprintf(stderr,"ERROR: close error on '%s'\n",old);
		ret = 0;
	}

	if (fclose(out) < 0) {
		fprintf(stderr,"ERROR: close error on '%s'\n",new);
		ret = 0;
	}

I can't think of any reason why a file opened for read would get longer
after it was read all the way to the end.  Has anyone else run into this
problem and found a way to fix it?  Thanks for any help that can be
offered.  I really like Megamax but this is strange.

Jerry Nowlin
(...!ihnp4!ihuxy!nowlin)

john@viper.UUCP (John Stanley) (05/15/87)

In article <1971@ihuxy.ATT.COM> nowlin@ihuxy.ATT.COM (Jerry Nowlin) writes:
 >
 >I've noticed a problem with Megamax and reading binary files.  The
 >following code is used to copy one file to another.  It works fine except
 >that the file being copied, the one being read, gets one byte longer after
 >every copy.
 >
 >	if ((in = fopen(old,"br")) == NULL) {
 >		fprintf(stderr,"ERROR: can't open '%s' to read\n",old);
 >		return 0;
 >	}
 >
 >	if ((out = fopen(new,"bw")) == NULL) {
 >		fprintf(stderr,"ERROR: can't open '%s' to write\n",new);
 >		fclose(in);
 >		return 0;
 >	}
...[rest of sample code]....

   Jerry, if you're trying to do "binary" file copys, you'd have better
luck (both reliability and (!)speed) if you used the raw mode disk io
functions and bipass the fopen/fread/fetc.. overhead entirely...

   The functions Fopen, Fread, etc are not 100% portable between systems,
but you're much less likely to get wierd effects if you use them...

--- 
John Stanley (john@viper.UUCP)
Software Consultant - DynaSoft Systems
UUCP: ...{amdahl,ihnp4,rutgers}!{meccts,dayton}!viper!john