问题是:我必须更改WAVE文件的标题,确切地说我必须更改ChunkSize和SubChunk2Size。问题是这些值使用4字节,但似乎使用fwrite我覆盖8个字节:
原文:
RIFFđ WAVEfmt
编辑:
RIFF(} } fmt
代码:
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 );
在已编辑的文件中WAVE
被覆盖。出了点问题,因为我从第4个字节开始写了4个字节,WAVE
从第8个字节开始。
我希望我至少有点清楚。这可以通过其他方式完成吗?
答案 0 :(得分:3)
好吧,根据我的man fopen
输出:
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.
话虽如此,我肯定会选择fopen("nova.wav", "r+b")
,因为w
似乎会截断文件,而您正在阅读之前阅读,而a
附加到文件,并且您想要重写文件的一部分。
答案 1 :(得分:2)
此代码在显示的每一行上至少有一个错误。
FILE *nova;
如果您使用open
,write
和lseek
而不是fopen
,{{1}执行此类操作,则更容易获得错误处理权限}和fwrite
。
fseek
第二个字符串应为nova=fopen ( "nova.wav", "wb" );
而不是"r+b"
,因此您不会截断该文件。你需要检查错误。
"wb"
您需要检查错误。
fseek ( nova, 4, SEEK_SET );
应始终使用第二个参数1和第三个参数调用 fwrite ( &brojacC,4,1,nova );
,该参数等于要写入的数据的大小;否则无法从短写中恢复。您需要检查短写和写错误。
您没有显示初始化fwrite
的代码,因此我无法评估您是否有任何字节顺序或结构填充问题,但我打赌您这样做。
brojacC
这对不相关的文件句柄fseek ( zvuk, 44, SEEK_SET );
而不是zvuk
进行操作。你需要检查错误。
nova
由于前一行的fwrite ( &brojacCS2,4,1,nova );
调用已应用于fseek
,因此写入偏移4 + 4 = 8,而不是预期的偏移44。上一行zvuk
行的所有评论也适用于此行。 (Psst:您需要检查错误。)
顺便说一下,逗号周围的间距不一致,邀请众神用闪电击打你。在括号内部放置空格也是如此。