Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1522931
  • 博文数量: 290
  • 博客积分: 3468
  • 博客等级: 中校
  • 技术积分: 3461
  • 用 户 组: 普通用户
  • 注册时间: 2010-12-28 22:21
文章分类

全部博文(290)

文章存档

2016年(13)

2015年(3)

2014年(42)

2013年(67)

2012年(90)

2011年(75)

分类: 嵌入式

2012-06-20 10:01:20

debug.h

点击(此处)折叠或打开

  1. #ifndef __DEBUG_H__
  2. #define __DEBUG_H__

  3. #define DEBUG 1

  4. #if DEBUG
  5. #include <stdio.h>
  6. #include <stdarg.h>

  7. #define DebugWhere() \
  8.     printf("FILENAME:%s,FUNCTION:%s,LINE:%d\n",\
  9.             __FILE__,__FUNCTION__,__LINE__):

  10. static inline void debug_p(const char *format,...)
  11. {
  12.     va_list args;

  13.     va_start(args,format);
  14.     vprintf(format,args);
  15.     va_end(args);
  16. }
  17. #else
  18. #define DebugWhere()
  19. static inline void debug_p(const char *format,...)
  20. {

  21. }
  22. #endif

  23. #endif

main.c

点击(此处)折叠或打开

  1. /* Copyright(C) 2012
  2.  * It's for free
  3.  */
  4. /**
  5.  * @file main.c
  6.  * @synopsis 读写文件
  7.  * @author sense, linuxboy2008@gmail.com
  8.  * @version 1.0
  9.  * @date 2012-06-20
  10.  */
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <unistd.h>
  15. #include "debug.h"

  16. /* --------------------------------------------------------------------------*/
  17. /**
  18.  * @synopsis open_file 打开文件
  19.  *
  20.  * @param filename 要打开的文件名
  21.  * @param mode 打开文件的方式
  22.  *
  23.  * @returns 文件流指针
  24.  */
  25. /* --------------------------------------------------------------------------*/
  26. FILE *open_file(char *filename,char *mode)
  27. {
  28.     FILE *fp = NULL;

  29.     fp = fopen(filename,mode);
  30.     if (NULL == fp) {
  31.         perror("open failure");
  32.         return NULL;
  33.     }

  34.     return fp;
  35. }

  36. /* --------------------------------------------------------------------------*/
  37. /**
  38.  * @synopsis get_file_size 获取文件的大小
  39.  *
  40.  * @param fp 文件流指针
  41.  *
  42.  * @returns 文件的大小
  43.  */
  44. /* --------------------------------------------------------------------------*/
  45. long get_file_size(FILE *fp)
  46. {
  47.     long len1 = 0;
  48.     long len2 = 0;
  49.     long length = 0;

  50.     fseek(fp,0,SEEK_SET);
  51.     len1 = ftell(fp);
  52.     fseek(fp,0,SEEK_END);
  53.     len2 = ftell(fp);

  54.     length = len2 - len1;
  55.     debug_p("len1:%ld,len2:%ld,length:%ld\n",len1,len2,length);
  56.     return length;
  57. }

  58. /* --------------------------------------------------------------------------*/
  59. /**
  60.  * @synopsis read_file 读指定文件的数据
  61.  *
  62.  * @param fp 文件流指针
  63.  * @param buf 存放读取的数据
  64.  *
  65.  * @returns 最后读入的字节数
  66.  */
  67. /* --------------------------------------------------------------------------*/
  68. int read_file(FILE *fp,char *buf)
  69. {
  70.     int nread = -1;

  71.     while (nread != 0) {
  72.         nread = fread(buf,1,4096,fp); //unix高编上说一次写入4K/8K的数据,效率较高
  73.         buf += nread;
  74.     }
  75.     debug_p("nread: %d\n",nread);
  76.     return nread;
  77. }

  78. /* --------------------------------------------------------------------------*/
  79. /**
  80.  * @synopsis write_file 将数据写入文件
  81.  *
  82.  * @param fp 文件流指针
  83.  * @param buf 要写入的数据
  84.  * @param size 写入数据的大小
  85.  *
  86.  * @returns 写入的字节数
  87.  */
  88. /* --------------------------------------------------------------------------*/
  89. int write_file(FILE *fp,char *buf,int size)
  90. {
  91.     int nwrite = 0;

  92.     nwrite = fwrite(buf,1,size,fp);
  93.     debug_p("nwrite: %d\n",nwrite);

  94.     return nwrite;
  95. }

  96. int main(int argc,char *argv[])
  97. {
  98.     char *in_filename = "test.yuv";
  99.     char *in_mode = "r";
  100.     char *out_filename = "test.jpg";
  101.     char *out_mode = "w";
  102.     char *data = NULL;
  103.     FILE *in_fp = NULL;
  104.     FILE *out_fp = NULL;
  105.     long file_size = 0;

  106.     in_fp = open_file(in_filename,in_mode);
  107.     file_size = get_file_size(in_fp);
  108.     data = (char *)malloc(file_size);
  109.     if (NULL == data) {
  110.         perror("malloc failure");
  111.         exit(1);
  112.     }
  113.     read_file(in_fp,data);
  114.     out_fp = open_file(out_filename,out_mode);
  115.     write_file(out_fp,data,file_size);

  116.     free(data);
  117.     data = NULL;

  118.     return 0;
  119. }

Makefile

点击(此处)折叠或打开

  1. #mips=1 ##将此项打开则编译为mipsel 格式的
  2. ifdef mips
  3. PREFIX=mipsel-linux-
  4. else
  5. PREFIX=
  6. endif

  7. CC=$(PREFIX)gcc
  8. AR=${PREFIX}ar

  9. TARGET=test
  10. CFLAGS=-g -Wall -O2
  11. Conditional_Compilation_CFLAGS=-D DEBUG=1
  12. LIBS=

  13. RM=rm -f

  14. SRC=$(wildcard *.c)
  15. OBJS=$(patsubst %.c,%.o, $(SRC))

  16. %.o:%.c
  17.     $(CC) $(CFLAGS) $(Conditional_Compilation_CFLAGS) -c $< -o $@
  18. $(TARGET):$(OBJS)
  19.     $(CC) -o $@ $^ ${LIBS}

  20. clean:
  21.     ${RM} $(wildcard *.o) $(TARGET)


阅读(1175) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~