#include <stdio.h>
#include <string.h>
#define MAX_RESULTS 100
main(int argc, char *argv[]){
FILE *f;
char path[50], string[50], S;
int i=0, j=0, k=-1, M, l[MAX_RESULTS], counter=0;
if (argc == 1){
printf("\nDwse to onoma tou arxeiou pros anagnwsh : ");
scanf("%s", path);
}
else
strcpy(path, argv[1]);
f = fopen(path, "r");
if (!f){ /*elegxos arxeiou*/
printf("\n\nSfalma kata to anoigma tou arxeiou %s\n\n", path);
return 1;
}
printf("\nDwse to string pros anazhthsh : ");
scanf("%s", string);
M = strlen(string);
while (!feof(f)){ /*epanalhpsh ws to telos tou arxeiou*/
S = fgetc(f); /*diabasma enos xarakthra apo to arxeio*/
counter++; /*metrhths gia to plithos twn xarakthrwn sto arxeio*/
j=0; /*deikths gia ton kathe xarakthra tou string pros anazhthsh*/
while ((j<=M) && (S==string[j])){ /*an isxuei h sugkrish twn xarakthrwn k*/
j++; /*eimaste sta oria tou mhkous tou string, aukshsh tou*/
S = fgetc(f); /*deikth xarakthra k diabasma tou epomenou*/
counter++;
} /*xarakthra apo to arxeio*/
if (j>=M){ /*an sigkrithikan thetika oloi oi xarakthres tou*/
k++; /*string k tou arxeiou tote exoume tauthsh k aukshsh tou*/
l[k]=counter-M; /*metrhth "k" gia to plithos eureshs apothikeush ths theshs*/
} /*pou brethike h tauthsh ston pinaka l*/
/* fseek(f, -j*sizeof(char), SEEK_CUR);*/ /*leitourgei mono se binary arxeia*/
}
printf("\n\nH leksh %s brethike %d fores sto arxeio %s, stis theseis : \n", string, k+1, path);
for(i=0;i<=k;i++)
printf("%d) %d\n", i+1, l[i]);
fclose(f);
}