Chinaunix首页 | 论坛 | 博客
  • 博客访问: 410247
  • 博文数量: 73
  • 博客积分: 3326
  • 博客等级: 中校
  • 技术积分: 631
  • 用 户 组: 普通用户
  • 注册时间: 2010-02-05 15:31
文章分类

全部博文(73)

文章存档

2014年(1)

2011年(51)

2010年(21)

分类: C/C++

2011-08-08 20:25:37

Read from text file (line.txt),the content :
==============================================
This is the first line.
Another line.
Another line.
Another line.
Another line.
Still more.
Almost done now --
Almost done now --
Another line.
Still more.
Finished!
==============================================
Requirements:
If two or more lines of text adjacent to the same content, then print out one line, others no need to print.
  1. #include <stdio.h>
  2. #include <string.h>

  3. #define MAX_LINE 128

  4. int main()
  5. {
  6.     FILE *fp = NULL;
  7.     char str[MAX_LINE] = {0};
  8.     char tmp[MAX_LINE] = {0};
  9.     long offset = 0;
  10.     fpos_t pos;
  11.     int flag = 0;

  12.     if(!(fp= fopen("line.txt","r+")))
  13.     {
  14.         printf("File open error!\n");
  15.         exit(0);
  16.     }
  17.     else
  18.     {
  19.         printf("Open file success!\n");
  20.      /* seek the beginning of the file */
  21.         //fseek(fp,0,SEEK_SET);
  22.         while(NULL != fgets(str,MAX_LINE,fp))
  23.         {
  24.             strcpy(tmp,str);
  25.             fgets(str,MAX_LINE,fp);
  26.             if(0 == strcmp(str,tmp))
  27.             {
  28.                 fputs(tmp,stdout);
  29.             }    

  30.         }
  31.     }
  32.     fclose(fp);    
  33.     return 0;
  34. }

  35. //some extent simple.
阅读(965) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~