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.
- #include <stdio.h>
-
#include <string.h>
-
-
#define MAX_LINE 128
-
-
int main()
-
{
-
FILE *fp = NULL;
-
char str[MAX_LINE] = {0};
-
char tmp[MAX_LINE] = {0};
-
long offset = 0;
-
fpos_t pos;
-
int flag = 0;
-
-
if(!(fp= fopen("line.txt","r+")))
-
{
-
printf("File open error!\n");
-
exit(0);
-
}
-
else
-
{
-
printf("Open file success!\n");
-
/* seek the beginning of the file */
-
//fseek(fp,0,SEEK_SET);
-
while(NULL != fgets(str,MAX_LINE,fp))
-
{
-
strcpy(tmp,str);
-
fgets(str,MAX_LINE,fp);
-
if(0 == strcmp(str,tmp))
-
{
-
fputs(tmp,stdout);
-
}
-
-
}
-
}
-
fclose(fp);
-
-
return 0;
-
}
- //some extent simple.
阅读(1001) | 评论(0) | 转发(0) |