#include<stdlib.h> #include<stdio.h> #include<string.h> #define height 256 #define width 256 int main() { unsigned char srcimg[height][width]; FILE* fpin; int i, j;

fpin = fopen("barbara512.raw", "rb");
if (fpin == NULL) {
    printf("Failed to open file.\n");
    return -1;
}

for (i = 0; i < height; i++) {
    for (j = 0; j < width; j++) {
        if (fread(&srcimg[i][j], 1, 1, fpin) != 1) {
            printf("Failed to read file.\n");
            fclose(fpin);
            return -1;
        }
        printf("%d", (int)srcimg[i][j]);
    }
}

fclose(fpin);

FILE* fpout;

fpout = fopen("D:/lena256.doc", "w+");
if (fpout == NULL) {
    printf("Failed to open file.\n");
    return -1;
}

for (i = 0; i < height; i++) {
    for (j = 0; j < width; j++) {
        if (fprintf(fpout, "%d\n", (int)srcimg[i][j]) < 0) {
            printf("Failed to write file.\n");
            fclose(fpout);
            return -1;
        }
    }
}

fclose(fpout);

fpout = fopen("D:/lena256_prc.raw", "w+");
if (fpout == NULL) {
    printf("Failed to open file.\n");
    return -1;
}

for (i = 0; i < height; i++) {
    for (j = 0; j < width; j++) {
        if (fwrite(&srcimg[i][j], 1, 1, fpout) != 1) {
            printf("Failed to write file.\n");
            fclose(fpout);
            return -1;
        }
    }
}

fclose(fpout);

return 0;

}

C语言代码:读取图像文件并写入文件

原文地址: https://www.cveoy.top/t/topic/ptb9 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录