1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
|
long * offset_indexor (FILE * file, const char * file_name){
// line numbers
unsigned long long nb_lines;
char cmd [1024];
sprintf(cmd,"wc -l %s",file_name);
FILE * tmp_FILE = popen (cmd, "r");
fscanf(tmp_FILE,"%llu", &nb_lines);
fclose(tmp_FILE);
fprintf(stderr,"%llu positions indexed in %s\n", nb_lines, file_name);
long * offset_index = (long *)malloc(sizeof(int)*nb_lines);
if(!offset_index){
fprintf(stderr,"Cannot allocate %llu longs; exit\n", nb_lines); exit(-1);
}
char c;
char s [512];
//rewind to top
fseek(file,0,SEEK_SET);
for(unsigned long i=0;i<nb_lines;i++){
offset_index[i]=ftell(file);
fscanf(file,"%[^\n]%c",s,&c);
}
return offset_index;
} |