#include <stdio.h> #include <stdlib.h> int findHead(FILE * ,int); int findLast(FILE * ,int); void deleteState(FILE **,int * ,int *); void deleteLine(FILE **,int ,int ); int main(){ FILE * fp = fopen("dict.dic","r"); int start,end; if(fp != NULL){ int a,b,c,d,e; fscanf(fp,"%d",&a); fscanf(fp,"%d",&b); fscanf(fp,"%d",&c); fscanf(fp,"%d",&d); fscanf(fp,"%d",&e); int mode; scanf("%d",&mode); switch (mode) { case 1: rewind(fp); start = findHead(fp,5); rewind(fp); end = findLast(fp,a); break; case 2: rewind(fp); start = findHead(fp,a); rewind(fp); end = findLast(fp,b); break; case 3: rewind(fp); start = findHead(fp,b); rewind(fp); end = findLast(fp,c); break; case 4: rewind(fp); start = findHead(fp,c); rewind(fp); end = findLast(fp,d); break; case 5: rewind(fp); start = findHead(fp,d); rewind(fp); end = findLast(fp,e); break; default: printf("??"); break; } rewind(fp); while(ftell(fp)<start) fgetc(fp); while(ftell(fp) != end){ int currentch = fgetc(fp); int flag = 1; if(currentch == '/' ){ int nextch = fgetc(fp); if(nextch == ''){ while(flag && ftell(fp) != end){ currentch = fgetc(fp); while(currentch == '' && ftell(fp) != end){ currentch = fgetc(fp); if(currentch == '/' && ftell(fp) != end) flag = 0; } } } else if(nextch == '/'){ while(currentch != '\n'){ currentch = fgetc(fp); } } else { printf("%c",currentch); currentch = nextch; } } if(ftell(fp) != end && flag) printf("%c",currentch); } } fclose(fp); return 0; } int findHead(FILE * fp,int line){ int ch = 0; while(line-- && ch != EOF){ ch = fgetc(fp); while(ch != '\n' && ch != EOF){ ch = fgetc(fp); } } return ftell(fp); } int findLast(FILE * fp,int line){ int ch = 0; while(line-- && ch != EOF){ ch = fgetc(fp); while(ch != '\n' && ch != EOF){ ch = fgetc(fp); } } return ftell(fp); }将子函数并到主函数里
时间: 2023-06-19 16:04:00 浏览: 187
C标准库函数集
5星 · 资源好评率100%
#include <stdio.h>
#include <stdlib.h>
int main(){
FILE * fp = fopen("dict.dic","r");
int start,end;
if(fp != NULL){
int a,b,c,d,e;
fscanf(fp,"%d",&a);
fscanf(fp,"%d",&b);
fscanf(fp,"%d",&c);
fscanf(fp,"%d",&d);
fscanf(fp,"%d",&e);
int mode;
scanf("%d",&mode);
switch (mode) {
case 1:
rewind(fp);
start = findHead(fp,5);
rewind(fp);
end = findLast(fp,a);
break;
case 2:
rewind(fp);
start = findHead(fp,a);
rewind(fp);
end = findLast(fp,b);
break;
case 3:
rewind(fp);
start = findHead(fp,b);
rewind(fp);
end = findLast(fp,c);
break;
case 4:
rewind(fp);
start = findHead(fp,c);
rewind(fp);
end = findLast(fp,d);
break;
case 5:
rewind(fp);
start = findHead(fp,d);
rewind(fp);
end = findLast(fp,e);
break;
default:
printf("??");
break;
}
rewind(fp);
while(ftell(fp)<start)
fgetc(fp);
while(ftell(fp) != end){
int currentch = fgetc(fp);
int flag = 1;
if(currentch == '/' ){
int nextch = fgetc(fp);
if(nextch == '\n'){
while(flag && ftell(fp) != end){
currentch = fgetc(fp);
while(currentch == ' ' && ftell(fp) != end){
currentch = fgetc(fp);
if(currentch == '/' && ftell(fp) != end)
flag = 0;
}
}
}
else if(nextch == '/'){
while(currentch != '\n'){
currentch = fgetc(fp);
}
}
else {
printf("%c",currentch);
currentch = nextch;
}
}
if(ftell(fp) != end && flag)
printf("%c",currentch);
}
}
fclose(fp);
return 0;
}
int findHead(FILE * fp,int line){
int ch = 0;
while(line-- && ch != EOF){
ch = fgetc(fp);
while(ch != '\n' && ch != EOF){
ch = fgetc(fp);
}
}
return ftell(fp);
}
int findLast(FILE * fp,int line){
int ch = 0;
while(line-- && ch != EOF){
ch = fgetc(fp);
while(ch != '\n' && ch != EOF){
ch = fgetc(fp);
}
}
return ftell(fp);
}
阅读全文