搜尋此網誌

2014年9月16日 星期二

Calculate the checksum of input file(計算輸入檔案的檢查和)

/****************************************************/

***Calculate the checksum of input file.

/****************************************************/

#include <stdio.h>

int main( int argc, char **argv )

{

        FILE *fp;

        int c;

        unsigned char checksum = 0;

        if (argc > 2){

                printf("The argument is fail..\n");

                return 1;

        }

//     printf(">>%d %s\n", argc, argv[1]);

        fp = fopen(argv[1], "rb");

        if (fp == NULL) {

                printf("%s can't open.\n", argv[1]);

                return 1;

        }

       

        while((c = fgetc(fp)) != EOF){

                checksum += (unsigned char)c;

        }

 

        printf("checksum = 0x%02X(0x%02X)\n", checksum, (unsigned char)((~checksum) + 1));

 

        fclose(fp);

        return 0;

}

沒有留言:

張貼留言