这个代码和du.c比较起来,没那么多对文件的操作。但还是看不太清楚。
先试着执行下。
建立文件1.test
1
1
2
2
3
3
3
4
然后,
./uniq -c 1.test
./uniq -d 1.test
./uniq -D 1.test
把命令的用法要搞个大致。再读代码。发现,其中因为在用uniq之前,命令行排好序了,所以现在只要比较相邻两行就够了,但其中比较算法很不明白。
while (!feof (istream))
{
char *thisfield;
size_t thislen;
if (readline (thisline, istream) == 0)
break;
thisfield = find_field (thisline);
thislen = thisline->length - 1 - (thisfield - thisline->buffer);
if (prevline->length == 0
|| different (thisfield, prevfield, thislen, prevlen))
{
fwrite (thisline->buffer, sizeof (char),
thisline->length, ostream);
SWAP_LINES (prevline, thisline);
prevfield = thisfield;
prevlen = thislen;
}
}
那个判断当前行和下一行是否相同,为什么要交换一下呢?不明白。
看了个大致。如果读代码象读小说一样就好了,读小说对我来说,是种最好的放松方式,但读代码,我也象读小说一样,先观其主要情节,多看几遍,慢慢深入。
prevfield = thisfield;
prevlen = thislen;
}
}
那个判断当前行和下一行是否相同,为什么要交换一下呢?不明白。
看了个大致。如果读代码象读小说一样就好了,读小说对我来说,是种最好的放松方式,但读代码,我也象读小说一样,先观其主要情节,多看几遍,慢慢深入。