100分悬赏简单C语言!!!

来源:百度知道 编辑:UC知道 时间:2024/06/05 07:13:47
做一道简单的输出一行文字的C语言题……
要求:1:输出不重复的行 2:如果重复,则只输出一行即可 3:虽然重复但如果不连续,则原样输出。4:用文件输出。

比如:输入文件如下:
Z:¥> type test.txt
We like dogs.
She likes cats.
He likes cats.
He likes cats.
He likes cats.
Mr.Peter eats tuna.
Mr.Peter eats scone.
Mr.Peter eats tuna

运行后要如下:
Z:¥> prog1 test.txt
We like dogs. (不重复所以输出)
She likes cats. (因为连续所以只输出一行)
He likes cats.
Mr.Peter eats tuna.
Mr.Peter eats scone.
Mr.Peter eats tuna. (虽然重复但不连续所以输出)

文件名找不到或者文件打不开的话要求输出这样的信息:

Z:¥> prog1
usage: Z:¥prog1.exe [input file] (文件名找不到)

Z:¥> prog1 test.tct
Z:¥prog1.exe: cannot open file "test.tct" for reading. (文件打不开)

就这些,谢谢指点……

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_LINE 1024

int main(int argc, char* argv[])
{
int i = 0;
char buffer[2][MAX_LINE];
FILE* input = NULL;
FILE* output = NULL;

if (argc == 1)
{
printf("usage: %s [input file].\n", argv[0]);
exit(1);
}
input = fopen(argv[1], "r");
if (input == NULL)
{
printf("%s: cannot open file \"%s\" for reading.\n", argv[0], argv[1]);
exit(1);
}
output = fopen("output.txt", "w");
while (!feof(input))
{
if(!fgets(buffer[i%2], MAX_LINE, input)) break;
if (strcmp(buffer[i%2], buffer[(i+1)%2]) != 0)
fprintf(output, "%s", buffer[i%2]);
i = i + 1;
}

return 0;
}