[comp.lang.c] writing a file byte by byte

6600bori@ucsbuxa.ucsb.edu (Boris Burtin) (03/01/91)

I'm using Turbo C 2.0 and DOS 3.3, and I'm attempting to write 8166 bytes
to a file (for those who know what I'm talking about, this is a MIDI sysex
file).  By the MIDIEX standard, this file should be 8166 bytes long, but
when I write it, it turns out to be some 8300 or so bytes long.  Does anyone
know how to solve this problem?  Thanks...

	- Boris Burtin

shaunc@gold.gvg.tek.com (Shaun Case) (03/01/91)

In article <9527@hub.ucsb.edu> 6600bori@ucsbuxa.ucsb.edu (Boris Burtin) writes:
>I'm using Turbo C 2.0 and DOS 3.3, and I'm attempting to write 8166 bytes
>to a file (for those who know what I'm talking about, this is a MIDI sysex
>file).  By the MIDIEX standard, this file should be 8166 bytes long, but
>when I write it, it turns out to be some 8300 or so bytes long.  Does anyone
>know how to solve this problem?  Thanks...
>

It's hard to tell without having some code to look at, but I suggest two things:

1)  make sure you have the file open in binary mode:
	fp = fopen(filename, "wb")
2)  use fwrite() to write out the binary contents of a structure containing 
    all the midi info.


-- 
Shaun Case:  shaunc@gold.gvg.tek.com  or  atman%ecst.csuchico.edu@RELAY.CS.NET 
 or Shaun Case of 1:119/666.0 (Fidonet)  or  1@9651 (WWIVnet)
---
A bullet in the Bush is worth two in the hand.

darcy@druid.uucp (D'Arcy J.M. Cain) (03/01/91)

In article <9527@hub.ucsb.edu> 6600bori@ucsbuxa.ucsb.edu (Boris Burtin) writes:
>I'm using Turbo C 2.0 and DOS 3.3, and I'm attempting to write 8166 bytes
>to a file (for those who know what I'm talking about, this is a MIDI sysex
>file).  By the MIDIEX standard, this file should be 8166 bytes long, but
>when I write it, it turns out to be some 8300 or so bytes long.  Does anyone
>know how to solve this problem?  Thanks...

There really isn't much information here but I'll make a guess.  How many
of the bytes that you are writing happens to be 0x0a?  By any chance 234?
maybe you are opening the file as "w" instead of "wb".  The default with
MSDOS is to open the file in text mode and that means that line feeds are
converted to CR/LF pairs.  If you are writing binary data you should always
open a file in binary mode even if you are working on a Unix system or some
other system where this doesn't matter.  It saves you heartaches when you
decide to port the code and also serves as sort of a comment about how you
intend to treat the data in the file.  Note that on systems where the "b"
flag is not needed the flag is ignored.

If this is not your problem please give more details and if possible some
sample code that exhibits the problem.

-- 
D'Arcy J.M. Cain (darcy@druid)     |
D'Arcy Cain Consulting             |   There's no government
West Hill, Ontario, Canada         |   like no government!
+1 416 281 6094                    |

garry@ceco.ceco.com (Garry Garrett) (03/02/91)

In article <9527@hub.ucsb.edu>, 6600bori@ucsbuxa.ucsb.edu (Boris Burtin) writes:
> I'm using Turbo C 2.0 and DOS 3.3, and I'm attempting to write 8166 bytes
> to a file (for those who know what I'm talking about, this is a MIDI sysex
> file).  By the MIDIEX standard, this file should be 8166 bytes long, but
> when I write it, it turns out to be some 8300 or so bytes long.  Does anyone
> know how to solve this problem?  Thanks...
> 
> 	- Boris Burtin


	Can you give us the exact size?  I suspect that DOS is rounding
up to the next full block.  I am not sure how big a block is for DOS, I
think that it is 512 bytes (1/2 K)  People in comp.os.msdos.programmer
might know better than the folks here.

(There, isn't that much nicer than flamming the guy for being in the
"Wrong" group?)