首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

在C下怎么改写文件数据

2012-02-11 
在C下如何改写文件数据a.img有1.44M,setup.bin有1024K,想将setup.bin写入a.img从512字节开始处忙了半天才

在C下如何改写文件数据
a.img有1.44M,setup.bin有1024K,想将setup.bin写入a.img从512字节开始处
忙了半天才发现下面的'wb'是清除原文件内容,相当于是在新建a.img

#include <stdio.h>
int main ( void )
{
  int ch;
  int k = 512;
  FILE *inf, *outf;
  inf = fopen( "setup.bin", "rb" ); //只读方式打开一个binary文件
  outf = fopen( "a.img","wb"); //只写方式打开一个binary文件
  fseek ( outf, k, SEEK_SET ); //指针指向从文件头开始K个字节
  ch = getc( inf );
  while ( ch != EOF )
  {
  putc(ch, outf); // 写入文件
  ch = getc( inf );
  }
  fclose(inf);
  fclose(outf);
  return 0;
}

请给出一个C下的改写文件程序吧,谢了。

[解决办法]
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.

[解决办法]
先把图片前54个字节(头)读出来,之后定位到要修改的位置,修改之后,重新写文件(头+具体数据)。

热点排行