C语言文件处理-对图片取模数据的转换

来源:互联网 发布:淘宝模特兼职价格 编辑:程序博客网 时间:2024/06/15 16:22

实现目标:

合并连续两个字节并取反。Example:

0xff,0x00,0x00,0xff,0xff,0xff----->0x0f,0xf0,0x00

使用命令行方式调用程序,该程序的实现代码如下:

#include <stdio.h>#include <stdlib.h>int main(int argc, char *argv[]){    char ch;    FILE *fp, *fo;  //fp:输入文件指针  fo:输出文件指针    long count = 0;    int i = 0;    char s[20] = {0};    //gets(s);    //puts(s);    printf("argc: %d \n", argc);   // printf("argv[0]: %s", argv[0]);    for(i = 0; i < argc; i++ )        printf("argv[%d]: %s\n",i, argv[i]);    if(argc != 3)       //三个形参,分别是程序名 源文件名 目标文件名    {        printf("Usage: %s source_filename target_filename\n", argv[0]);        exit(1);    }    if((fp = fopen(argv[1], "r")) == NULL)    {        printf("Can't open %s\n", argv[1]);        exit(1);    }    if((fo = fopen(argv[2], "w")) == NULL)    {        printf("Can't creat %s\n", argv[2]);        exit(1);    }    while((ch = getc(fp))!= EOF)        // 0    {        if(ch == '\n')        {            //printf("我遇到了回车!\n");            //fputc('\n',fo);        }        else        {             if( ch == '0')            if(ch = getc(fp) == 'x')        // x            {                if(ch = getc(fp) == 'f')    //第一个字节是0xff                {                    ch = getc(fp);      // f                    ch = getc(fp);      // ,                    ch = getc(fp);      // '空格'                    ch = getc(fp);      // 0                    ch = getc(fp);      // x                    if(ch = getc(fp) == 'f')//第二个字节是0xff                    {                         putc('0', fo);putc('x', fo);putc('0', fo);putc('0', fo);putc(',', fo);putc(' ', fo);                    }                    else                    //第二个字节是0x00                    {                        putc('0', fo);putc('x', fo);putc('0', fo);putc('f', fo);putc(',', fo);putc(' ', fo);                    }                }                else                        //第一个字节是0x00                {                    ch = getc(fp);      // 0                    ch = getc(fp);      // ,                    ch = getc(fp);      // '空格'                    ch = getc(fp);      // 0                    ch = getc(fp);      // x                    if(ch = getc(fp) == 'f')//第二个字节是0xff                    {                         putc('0', fo);putc('x', fo);putc('f', fo);putc('0', fo);putc(',', fo);putc(' ', fo);                    }                    else                    //第二个字节是0x00                    {                        putc('0', fo);putc('x', fo);putc('f', fo);putc('f', fo);putc(',', fo);putc(' ', fo);                    }                }            }            ch = getc(fp);  // 0/f            ch = getc(fp);  // ,            ch = getc(fp);  // 空格        }    }}


0 0