69,382
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXL 1000
int L;
static char ln[MAXL][10000];
int i;
int j;
int s;
FILE *f;
int main(int argc,char **argv) {
if (argc<3) {
printf("Usage: %s filename lines\n",argv[0]);
return 1;
}
f=fopen(argv[1],"r");
if (NULL==f) {
printf("Can not open file [%s]!\n",argv[1]);
return 2;
}
L=atoi(argv[2]);
if (L<2 || 1000<L) L=2;
s=0;
i=0;
while (1) {
if (NULL==fgets(ln[i],10000,f)) break;
if (strstr(ln[i],"error")) {
if (s==0) {
for (j=1;j<L;j++) printf("%s",ln[(i+j)%L]);
s=L;
}
}
if (s>0) {
printf("%s",ln[i]);
s--;
}
i=(i+1)%L;
}
fclose(f);
return 0;
}
#include <stdio.h>
#include <string.h>
static char ln[3][10000];
int i,s;
FILE *f;
int main() {
f=fopen("input.txt","r");
if (NULL==f) {
printf("Can not open file input.txt!\n");
return 1;
}
s=0;
i=0;
while (1) {
if (NULL==fgets(ln[i],10000,f)) break;
if (strstr(ln[i],"error")) {
if (s==0) {
printf("%s",ln[(i+1)%3]);
printf("%s",ln[(i+2)%3]);
s=3;
}
}
if (s>0) {
printf("%s",ln[i]);
s--;
}
i=(i+1)%3;
}
fclose(f);
return 0;
}