簡體   English   中英

我需要使用緩沖區以c語言復制位圖圖像文件

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

我必須使用緩沖區復制位圖圖像文件。 這是我需要做的一個例子。 我必須先立即將位圖的不同部分讀入緩沖區,然后將其寫入目標文件。 當我將不同的部分讀入緩沖區時,前一個字符串被覆蓋,並且只寫入讀取的最后一個字符串。 我不想對每個必須編寫的部分使用讀寫功能。 請幫我解釋一下代碼。

#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();
}

首先,我將嘗試解決您的具體問題,並避免對代碼中的其他內容發表任何評論。

看起來fread()fwrite()語句不正確。 以下代碼可能更准確。

   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.

fread()fwrite()格式是每K&R,第二版,第247頁。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM