免费注册 查看新帖 |

Chinaunix

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

[C] printf问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2013-07-11 11:12 |只看该作者 |倒序浏览
今天使用printf打印的时候,发现一个很奇怪的问题:
代码:
                        printf("%lu\t\" %s\" != \"%s\"\n", lineNum, buff1, buff2);
                        printf("%s\n", buff1);
                          printf("%s \n", buff2);

实际打印值:

"!= "idasdfadafnt main(int argc, char **argv)
int main(int argc, char **argv)
dasdfadafnt main(int argc, char **argv)

即在第一行打印中并没有将buff1的内容打印出来,而后边却可以打印出来,分析了一会,第一句printf也没有发现什么问题,是何道理?

---源代码----
  1. /* Exercise 7-6 - write a program to compare two files, printing the first line
  2. * where they differ
  3. *
  4. * Note : I amended this a bit...if a file is shorter than the other, but is identical
  5. * up to that point, the program prints out "EOF" as the string that's not equal.
  6. *
  7. */

  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>

  11. #define BUFF_SIZE 1000

  12. /* uses fgets, removes the '\n' at the end of the string if it exists */
  13. char *safegets(char *buffer, int length, FILE *file)
  14. {
  15.         char *ptr;
  16.         int len;
  17.         
  18.         if (buffer != NULL)
  19.         {
  20.                 ptr = fgets(buffer, length, file);
  21.                
  22.                 if (ptr != NULL)
  23.                 {
  24.                         len = strlen(buffer);
  25.                
  26.                         if (len > 0)
  27.                         {
  28.                                 if (buffer[len - 1] == '\n')
  29.                                 {
  30.                                         buffer[len - 1] = '\0';
  31.                                 }
  32.                         }
  33.                 }
  34.                
  35.                 return ptr;
  36.         }      
  37.         
  38.         return NULL;
  39. }

  40. int main(int argc, char *argv[])
  41. {
  42.         FILE *leftFile, *rightFile;
  43.         char buff1[BUFF_SIZE], buff2[BUFF_SIZE];
  44.         char *ptr1, *ptr2;
  45.         unsigned long lineNum = 0;
  46.         
  47.         if (argc < 3)
  48.         {
  49.                 fprintf(stderr, "Usage : 7_6 <path to file> <path to file>\n");
  50.                 return 0;
  51.         }
  52.         
  53.         if (!(leftFile = fopen(argv[1], "r")))
  54.         {
  55.                 fprintf(stderr, "Couldn't open %s for reading\n", argv[1]);     
  56.                 return 0;
  57.         }
  58.         
  59.         if (!(rightFile = fopen(argv[2], "r")))
  60.         {
  61.                 fprintf(stderr, "Couldn't open %s for reading\n", argv[2]);
  62.                 fclose(leftFile); /* RJH 10 Jul 2000 */
  63.                 return 0;
  64.         }
  65.         
  66.         /* read through each file, line by line */
  67.         ptr1 = safegets(buff1, BUFF_SIZE, leftFile);
  68.         ptr2 = safegets(buff2, BUFF_SIZE, rightFile);
  69.         ++lineNum;
  70.         
  71.         /* stop when either we've exhausted either file's data */
  72.         while (ptr1 != NULL && ptr2 != NULL)
  73.         {
  74.                 /* compare the two lines */
  75.                 if (strcmp(buff1, buff2) != 0)
  76.                 {
  77.                         printf("Difference:\n");
  78.                         printf("%lu\t\" %s\" != \"%s\"\n", lineNum, buff1, buff2);
  79.                         printf("%s\n", buff1);
  80.                           printf("%s \n", buff2);
  81.                         goto CleanUp;
  82.                 }
  83.                
  84.                 ptr1 = safegets(buff1, BUFF_SIZE, leftFile);
  85.                 ptr2 = safegets(buff2, BUFF_SIZE, rightFile);
  86.                 ++lineNum;
  87.         }      

  88.         /*
  89.          * if one of the files ended prematurely, it definitely
  90.          * isn't equivalent to the other
  91.          */
  92.         if (ptr1 != NULL && ptr2 == NULL)
  93.         {
  94.                 printf("Difference:\n");
  95.                 printf("%lu\t\"%s\" != \"EOF\"\n", lineNum, buff1);
  96.         }      
  97.         else if (ptr1 == NULL && ptr2 != NULL)
  98.         {
  99.                 printf("Difference:\n");
  100.                 printf("%lu\t\"EOF\" != \"%s\"\n", lineNum, buff2);
  101.         }
  102.         else
  103.         {
  104.                 printf("No differences\n");
  105.         }

  106. CleanUp:

  107.         fclose(leftFile);
  108.         fclose(rightFile);      
  109.         return EXIT_SUCCESS;   
  110. }
复制代码

论坛徽章:
4
水瓶座
日期:2013-09-06 12:27:30摩羯座
日期:2013-09-28 14:07:46处女座
日期:2013-10-24 14:25:01酉鸡
日期:2014-04-07 11:54:15
2 [报告]
发表于 2013-07-11 12:57 |只看该作者
字符串里有\r吧, 重定向到文件里就看到了.
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP