Reputation: 257
Here is the problem: I have to change header of WAVE file, to be exact I have to change ChunkSize and SubChunk2Size. The problem is that those values use 4bytes but it seemt that using fwrite i overwrite 8 bytes:
the original:
RIFFđ WAVEfmt
edited:
RIFF(} } fmt
code:
FILE *nova;
nova=fopen ( "nova.wav", "wb" );
fseek ( nova, 4, SEEK_SET );
fwrite ( &brojacC,4,1,nova );
fseek ( zvuk, 44, SEEK_SET );
fwrite ( &brojacCS2,4,1,nova );
In edited file WAVE
is overwritten. Something went wrong because I started at 4th byte and wrote 4 bytes and WAVE
starts at 8th byte.
I hope I was at least a bit clear. Can this be done in some other way?
Upvotes: 1
Views: 4144
Reputation: 140796
This code has at least one bug on every line shown.
FILE *nova;
It is easier to get the error handling right if you do this sort of thing with open
, write
, and lseek
rather than fopen
, fwrite
, and fseek
.
nova=fopen ( "nova.wav", "wb" );
The second string should be "r+b"
instead of "wb"
so you don't truncate the file. You need to check for errors.
fseek ( nova, 4, SEEK_SET );
You need to check for errors.
fwrite ( &brojacC,4,1,nova );
fwrite
should always be called with second argument 1 and third argument equal to the size of the data to be written; otherwise it is impossible to recover from short writes. You need to check for short writes and write errors.
You don't show the code that initializes brojacC
so I can't assess whether you have any endianness or structure-padding problems, but I bet you do.
fseek ( zvuk, 44, SEEK_SET );
This operates on the unrelated file handle zvuk
rather than nova
. And you need to check for errors.
fwrite ( &brojacCS2,4,1,nova );
Since the fseek
call on the previous line was applied to zvuk
, this writes at offset 4+4=8, not offset 44 as was intended. All the comments on the previous fwrite
line also apply to this line. (Psst: You need to check for errors.)
Inconsistent spacing around commas, by the way, invites the gods to strike you with lightning. So does putting spaces on the inside of your parentheses.
Upvotes: 2
Reputation: 1312
Well, according to my man fopen
output:
r Open text file for reading. The stream is positioned at the
beginning of the file.
r+ Open for reading and writing. The stream is positioned at the
beginning of the file.
w Truncate file to zero length or create text file for writing.
The stream is positioned at the beginning of the file.
w+ Open for reading and writing. The file is created if it does
not exist, otherwise it is truncated. The stream is positioned
at the beginning of the file.
a Open for appending (writing at end of file). The file is cre‐
ated if it does not exist. The stream is positioned at the end
of the file.
a+ Open for reading and appending (writing at end of file). The
file is created if it does not exist. The initial file position
for reading is at the beginning of the file, but output is
always appended to the end of the file.
That being said, I would definitely go forfopen("nova.wav", "r+b")
, as w
seems to truncate the file, and you're reading before writing, while a
appends to the end of the file, and you want to rewrite part of the file.
Upvotes: 3