免费注册 查看新帖 |

Chinaunix

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

[C] 请教一个问题。文件读入的问题。 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-12-10 20:50 |只看该作者 |倒序浏览
题目大意如下:
有一段文本,比如
this is the first line.
another line.
another line.
another line.
another line.
another line.
still more.
Almost done now---.
Almost done now---.
another line.
still more.
finished.
如果有重复的两行,那么需要打印这两行。
another line.
Almost done now---.
其余不需要打印。


  1. #include    <string.h>
  2. #include        <stdio.h>
  3. #include        <stdlib.h>
  4. #include        "../type.h"


  5.     int
  6. main ( int argc, char *argv[] )
  7. {
  8.     FILE *pfFile = NULL;
  9.     int iCh;
  10.     int i;
  11.     char acRead[MAX_DATA_LEN];
  12.     char acStore[MAX_DATA_LEN];
  13.     char acPrint[MAX_DATA_LEN];


  14.     if (NULL == (pfFile = fopen("a.txt","rw")))
  15.     {
  16.         printf("can't open file\n");
  17.         return EXIT_FAILURE;
  18.     }
  19.       
  20.     while (EOF != (iCh = getchar()))
  21.     {
  22.         i = 0;

  23.         strncpy(acStore, acRead, MAX_DATA_LEN);
  24.         strncpy(acPrint, acRead, MAX_DATA_LEN);

  25.         while('\n' != (iCh = getchar()))
  26.         {
  27.             acRead[i] = iCh;
  28.             i++;
  29.         }

  30.         printf("%s\n", acRead);

  31.         if (0 != strcmp(acRead, acStore)
  32.             && 0 != strcmp(acRead, acPrint))
  33.         {
  34.             printf("%s \n", acRead);
  35.             strncpy(acPrint, acRead, MAX_DATA_LEN);
  36.         }
  37.     }

  38.     return EXIT_SUCCESS;
  39. }                                /* ----------  end of function main  ---------- */
复制代码


a.txt 如下
this is the first line.
another line.
another line.
another line.
another line.
another line.
still more.
Almost done now---.
Almost done now---.
another line.
still more.
finished.

为什么我每次用gdb调试,第一个字符总是‘s’,然后就跳到了printf("%s\n", acRead);这条语句。其中的某一行根本没有读取?

论坛徽章:
0
2 [报告]
发表于 2009-12-10 20:57 |只看该作者
补充一下:
os:ubuntu8.10
编译器:gcc 版本 4.3.2 (Ubuntu 4.3.2-1ubuntu12)

论坛徽章:
0
3 [报告]
发表于 2009-12-10 22:48 |只看该作者
同志们帮忙看看,多谢。

论坛徽章:
0
4 [报告]
发表于 2009-12-10 23:06 |只看该作者
shell解法

  1. awk '{a[$0]++}END{for(i in a){if(a[i]>1)print i}}' urfile
复制代码


c解法
思路
fgets取一行到str将str散列了一个int
hash(str)---num   
if(Hash[num]==0)
    Hash[num]++;
else if(Hash[num]==1)
  {
   printf...
   Hash[num]++;
}

没考虑冲突的情况

论坛徽章:
0
5 [报告]
发表于 2009-12-10 23:16 |只看该作者
何不用getline呢?
而且还是getchar(),这个是从标准输入读

论坛徽章:
0
6 [报告]
发表于 2009-12-10 23:23 |只看该作者
随便帮你改了改,要回去睡觉了

  1. #include    <string.h>
  2. #include    <stdio.h>
  3. #include    <stdlib.h>
  4. #define MAX_DATA_LEN 80

  5. int main( int argc, char *argv[] )
  6. {
  7.   FILE *pfFile = NULL;

  8.   int iCh;

  9.   int i;

  10.   char acRead[MAX_DATA_LEN] = { '\0' };

  11.   char acStore[MAX_DATA_LEN];

  12.   char acPrint[MAX_DATA_LEN];


  13.   if( NULL == ( pfFile = fopen( "a.txt", "rw" ) ) ) {
  14.     printf( "can't open file\n" );
  15.     return EXIT_FAILURE;
  16.   }

  17.   while( EOF != ( iCh = getc( pfFile ) ) ) {
  18.     i = 0;

  19.     strncpy( acStore, acRead, MAX_DATA_LEN );

  20.     acRead[i++] = iCh;
  21.     while( '\n' != ( iCh = getc( pfFile ) ) ) {
  22.       acRead[i++] = iCh;
  23.     }
  24.     acRead[i] = '\0';

  25.   //printf( "%s\n", acRead );

  26.     if( 0 == strcmp( acRead, acStore )
  27.         && 0 != strcmp( acRead, acPrint ) ) {
  28.       printf( "%s \n", acRead );
  29.       strncpy( acPrint, acRead, MAX_DATA_LEN );
  30.     }
  31.   }

  32.   return EXIT_SUCCESS;
  33. }                                /* ----------  end of function main  ---------- */
复制代码

1.getchar用错了
2.第一个strcmp用错了
3.一开始要将acRead初始化成\0(第一次的比较有点特殊)
4.第一个字符要吐出来
5.字符串要以\0结尾


kernel@fairland:/tmp$ ./att
another line.
Almost done now---.


可取之处:
1.用strncpy而不是用strcpy可以防止buffer overflow
2. 1 == strcmp() 这样写也可以防止写错

[ 本帖最后由 churchmice 于 2009-12-10 23:26 编辑 ]

论坛徽章:
0
7 [报告]
发表于 2009-12-11 11:19 |只看该作者
这个很像我们学校保研的上机题目
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP