简体   繁体   English

我需要使用缓冲区以c语言复制位图图像文件

[英]i need to copy bitmap image file in c language using a buffer

i have to copy a bitmap image file using a buffer. 我必须使用缓冲区复制位图图像文件。 here is an example of what i need to do. 这是我需要做的一个例子。 i have to read different parts of a bitmap into the buffer first at once and then write it to the target file. 我必须先立即将位图的不同部分读入缓冲区,然后将其写入目标文件。 when i read different parts into the buffer , the previous string gets overwritten and the last string that is read is only written. 当我将不同的部分读入缓冲区时,前一个字符串被覆盖,并且只写入读取的最后一个字符串。 i dont want to use read and write function for every part that has to be written. 我不想对每个必须编写的部分使用读写功能。 please help me with the code. 请帮我解释一下代码。

#include <conio.h>
#include <stdio.h>
#include <stdlib.h>

void fskip(FILE *fp, int num_bytes) {
  int i;
  for (i = 0; i < num_bytes; i++)
    fgetc(fp);
}

int main() {
  FILE *fp, *fp1;

  fp = fopen("c:\\users\\tapan\\desktop\\splash.bmp", "rb");
  fp1 = fopen("c:\\users\\tapan\\desktop\\splash2.bmp", "wb");

  int *j;
  j = (int *)malloc(3000);

  int k = 223121;
  int *i = &k;

  fread(j, 2, 1, fp);

  fread(j, 10, 1, fp);
  fwrite(j, 12, 1, fp1);

  fclose(fp1);
  fclose(fp);

  getch();
}

First of all, I will try to address your specific question and avoid any comments about other things in the code. 首先,我将尝试解决您的具体问题,并避免对代码中的其他内容发表任何评论。

It looks like the fread() and fwrite() statements are not correct. 看起来fread()fwrite()语句不正确。 The following code might be more exact. 以下代码可能更准确。

   int main() {
     FILE *fp, *fp1;

     fp = fopen("c:\\users\\tapan\\desktop\\splash.bmp", "rb");
     fp1 = fopen("c:\\users\\tapan\\desktop\\splash2.bmp", "wb");

     int *j;
     j = (int *) malloc(3000);

     int k = 223121;
     int *i = &k;

     // read 2 items of sizeof(int) into j from fp
     fread(j, sizeof(int), 2, fp);

     // read 10 items of sizeof(int) into j + 2 from fp
     fread(j+2, sizeof(int), 10, fp);

     // write 12 items of sizeof(int) from j to fp1
     fwrite(j, sizeof(int), 12, fp1);

     fclose(fp1);
     fclose(fp);

     getch();
   }
   // Note. The above code has NOT been tested, it is thrown-up here for discussion.

Format of fread() and fwrite() is per K&R, second edition, page 247. fread()fwrite()格式是每K&R,第二版,第247页。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM