C:编辑二进制文件

标签 c file-io binary wav

问题是:我必须更改 WAVE 文件的 header ,准确地说,我必须更改 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 个字节开始。

我希望我至少说清楚了一点。这可以通过其他方式完成吗?

最佳答案

好吧,根据我的 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 附加到文件的末尾,你想重写文件的一部分。

关于C:编辑二进制文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8874550/

相关文章:

c# - 读取文件的特定字节

binary - 从二进制转换为 IEEE 浮点

database-design - 在 PostgreSQL 中存储二进制文件的多个修订版的最有效方法是什么?

c - C中的按位符号除法算法

c - 指针问题和困惑

c - 根据字符位置将字符串分成两部分

php - 如何从 PHP 调用 c 函数?

c++ - 拦截windows打开的文件

c - 使用 Unix 系统调用插入文本

c - 为 Pthreads 移植 GNU Pth 代码有什么注意事项吗?