- 论坛徽章:
- 0
|
干脆把代码放出来,谁都会写这种代码。。。
- /*
- This function calculate size of the input file to the output file.
- */
- #include <stdio.h>
- #define SIZE_BUF_SIZE 4
- int main(int argc, char *argv[])
- {
- FILE *f_in;
- FILE *f_out;
- long size=0;
- unsigned char size_buf[SIZE_BUF_SIZE];
- char *pu8SrcBuffer;
- // option check
- if(argc != 4){
- printf("\n");
- printf("Usage: %s {option} {input file}\n", argv[0]);
- printf("\n");
- printf("option: -b -> binary \n");
- printf("\n");
- return 0;
- }
- if(argv[1][0] == '-' && argv[1][2] == '\0'){ //processing option
- switch(argv[1][1]){
- case 'b':
- break;
- default :
- printf("Wrong option!\n");
- return 0;
- }
- }
- else{
- printf("Wrong option!\n");
- return 0;
- }
- // get input file size
- f_in = fopen(argv[2], "rb");
- if(f_in == NULL) {
- printf("open file failed\n");
- return 0;
- }
- fseek( f_in, 0, SEEK_END );
- size = ftell( f_in );
- // transfer size
- size_buf[3] = ((size>>24)&0xFF);
- size_buf[2] = ((size>>16)&0xFF);
- size_buf[1] = ((size>>8)&0xFF);
- size_buf[0] = (size&0xFF);
- // malloc buffer
- pu8SrcBuffer = (char *)malloc(size);
- if(NULL ==pu8SrcBuffer)
- {
- printf("Can not alloc buffer(size=0x%x) for appending\n", size);
- fclose(f_in);
- return 0;
- }
- // seek src to begin
- if( 0 != fseek(f_in, 0, SEEK_SET) )
- {
- printf("Can not seek \"%s\" for read\n", argv[2]);
- fclose(f_in);
- return 0;
- }
- if( size != fread(pu8SrcBuffer, 1, size,f_in) )
- {
- printf("Read \"%s\" failed\n", argv[2]);
- fclose(f_in);
- return 0;
- }
- fclose(f_in);
- // open target
- f_out = fopen(argv[3], "wb+");
- if(f_out == NULL) {
- printf("Can not open \"%s\" for appending\n", argv[3]);
- return 0;
- }
- fwrite(size_buf, 1, SIZE_BUF_SIZE, f_out);
- fwrite(pu8SrcBuffer, 1, size, f_out);
- free(pu8SrcBuffer);
- fclose(f_out);
- return 0;//该行之前没有,这里是我添加的
- }
复制代码 可以帮我试试在linux环境下情况是怎样的?
打开或者注释掉最后一句return 0;分别编译成两个不同的可执行档,然后在命令行直接执行之后使用echo $?查看该执行档返回给shell的值。
也可以写个简单的makefile
test:
<tab>@add_size -b inputBinaryFileName outputBinaryFileName
<tab>@echo "If go here, success, else failure"
保存为makefile,在命令行敲入make |
|