免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2216 | 回复: 8
打印 上一主题 下一主题

是少了一括号!!!! [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-03-03 15:52 |只看该作者 |倒序浏览
本帖最后由 ljpdxj 于 2012-03-03 16:17 编辑

source code如附件


源码如下:
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <getopt.h>
  5. #include <time.h>
  6. #include <sys/stat.h>
  7. #include <sys/wait.h>
  8. #include <stdint.h>
  9. #include <errno.h>

  10. #define VERSION "1.0.0"

  11. #define PACKMOD_DEBUG

  12. #ifndef PACKMOD_DEBUG
  13. #define DBG(s...)
  14. #else
  15. #define DBG(fmt, arg...)        printf("[Debug: createRomBin %4d] "fmt, __LINE__, ##arg)
  16. #endif

  17. #define ERR(fmt, arg...)                                                                \
  18. do {                                                                                                \
  19.         printf("[Error!!! createRomBin %d] "fmt, __LINE__, ##arg);        \
  20. } while (0)

  21. #define WORKING_BUF_SIZE                4096
  22. char u8WorkBuf[WORKING_BUF_SIZE];
  23. size_t uReadBytes = 0;

  24. static void createRomBin(void)
  25. {
  26.         FILE *pFirstBootFile = NULL;
  27.         FILE *pOutPutFile = NULL;
  28.         struct stat statbuf;

  29.         /* open first boot file */
  30.         pFirstBootFile = fopen("./256k", "rb+");
  31.         //printf("pGlobalInfo->pFbName:%s\n", pGlobalInfo->pFbName);
  32.         if (pFirstBootFile == NULL)
  33.         {
  34.                 ERR("Can not open \"%s\" for read\n", "./256k");
  35.                 return ;
  36.         }
  37.         fstat(fileno(pFirstBootFile), &statbuf);
  38.         printf("=========>file leng:%d\n", statbuf.st_size);

  39.         /* open output file */
  40.         pOutPutFile = fopen("./test", "wb+");
  41.         printf("pGlobalInfo->pOFName:%s\n", "./test");
  42.         if (pOutPutFile == NULL)
  43.         {
  44.                 ERR("Can not open \"%s\" for write\n", "./test");
  45.                 return;
  46.         }

  47.         /* copy data to output file */
  48.         while (uReadBytes = fread(u8WorkBuf, 1, WORKING_BUF_SIZE, pFirstBootFile) != 0)
  49.         {
  50.                 printf("read bytes:%d\n", uReadBytes);
  51.                 fwrite(u8WorkBuf, 1, WORKING_BUF_SIZE, pOutPutFile);
  52.         }

  53.         fclose(pFirstBootFile);
  54.         fclose(pOutPutFile);
  55. }

  56. int main(int argc, char *argv[])
  57. {
  58.         createRomBin();

  59.         return 0;
  60. }

复制代码
c档是砍掉了不相关的函数之后的,另外一个二进制档会被此c档使用到。

why??????fread一直返回1,到文件结束?????

tools.rar

952 Bytes, 下载次数: 13

论坛徽章:
0
2 [报告]
发表于 2012-03-03 16:02 |只看该作者
上面的地方
fwrite(u8WorkBuf, 1, WORKING_BUF_SIZE, pOutPutFile);

第三个参数写成了buffer的size,可以看到输出文件与输入文件一致

但是上行的打印
printf("read bytes:%d\n", uReadBytes);
始终为1
虽然buffer里面是已经读出来的WORKING_BUF_SIZE * 1长的data。

当把
fwrite(u8WorkBuf, 1, WORKING_BUF_SIZE, pOutPutFile);
修改为
fwrite(u8WorkBuf, 1, uReadBytes, pOutPutFile);
之后,
输出文件只有几bytes.......

本人自认为4年工作经验,怎么搞不懂这问题。。。。

论坛徽章:
0
3 [报告]
发表于 2012-03-03 16:13 |只看该作者
uReadBytes = fread(u8WorkBuf, 1, WORKING_BUF_SIZE, pFirstBootFile) != 0

等价于uReadBytes = (fread(u8WorkBuf, 1, WORKING_BUF_SIZE, pFirstBootFile) != 0)

understand ?

论坛徽章:
0
4 [报告]
发表于 2012-03-03 16:16 |只看该作者
日本人!!!!!!!!!1

太失败了。。。。

少了一括号!!!!!!!!!!!!!!!

论坛徽章:
0
5 [报告]
发表于 2012-03-03 16:17 |只看该作者
回复 3# sanbiangongzi


    谢谢楼上!!!!!

论坛徽章:
0
6 [报告]
发表于 2012-03-03 18:08 |只看该作者
fread的返回值size_t是指读回来的Element个数. fwrite也是.

while (uRead = fread(u8WorkBuf, WORKING_BUF_SIZE, 1, pFirstBootFile) != 0)
{
                printf("read bytes:%d\n", uRead *WORKING_BUF_SIZE);
                fwrite(u8WorkBuf, WORKING_BUF_SIZE, 1,  pOutPutFile);
}

论坛徽章:
8
申猴
日期:2014-01-01 22:11:07白羊座
日期:2014-11-18 20:53:022015年辞旧岁徽章
日期:2015-03-03 16:54:1515-16赛季CBA联赛之四川
日期:2016-01-19 18:39:36综合交流区版块每日发帖之星
日期:2016-06-07 06:20:0015-16赛季CBA联赛之广东
日期:2016-10-30 11:34:40CU十四周年纪念徽章
日期:2016-11-13 10:06:5715-16赛季CBA联赛之同曦
日期:2022-08-28 15:58:19
7 [报告]
发表于 2012-03-03 21:57 |只看该作者
lol

论坛徽章:
0
8 [报告]
发表于 2012-03-04 15:11 |只看该作者
一个我也看不懂啊

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
9 [报告]
发表于 2012-03-04 15:59 |只看该作者
四年工作经验还是这德行,说明从来没有受过正式的软件开发训练。

在我的 team 里,赋值语句必须是单独成行的。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP