Can't navigate through a file using fseek (C) -
i'm trying navigate through file until find 2 rows first 4 digits of first different first 4 of next one. i've been using fseek navigate trough until find sequence want. it's list of population decade, , each decade there more 1 line, in sequence, oldest newest year. , use ftell in main first offset can start reading in middle of file.
the problem here keeps going backwards when offset positive , should forwards.
the lines should make go forward this:
encontrar_inicio(f, ano, offset/2); and shows:
1980 é maior que 1950 ano maior, anda para frente 1980 é maior que 1910 ano maior, anda para frente 1980 é maior que 1900 ano maior, anda para frente 1980 é maior que 1880 ano maior, anda para frente 1980 é maior que 1860 ano maior, anda para frente it says 1980 bigger [year], go forward. doesn't. , after printf gives line line i've posted above.
and whole function:
void encontrar_inicio (file *f, int ano, long int offset) { int i, meio, ano2; char aux[100], aux2[100], aux3[100], lixo[100]; //printf("%lu\n", offset); fseek( f, offset, seek_set); fgets(aux, 100, f); fgets(aux2, 5, f); ano2 = atoi(aux2); if(ano==ano2) { printf("%d é igual %d anos iguais, anda para trás\n", ano, ano2); encontrar_inicio(f, ano, -offset/2);//go in file } if(ano != ano2) { if(ano < ano2) { printf("%d é menor q %d ano menor, anda para trás\n", ano, ano2); encontrar_inicio(f, ano, -offset/2);//go in file } if(ano > ano2) { printf("aqui\n"); fgets(lixo, 100, f);//next line fgets(aux3, 5, f); int ano3 = atoi(aux3); if(ano==ano3) { printf("%d é igual %d encontrou o ano, começa ler\n", ano, ano3); return; //começa ler } if(ano!=ano3) { printf("%d é maior que %d ano maior, anda para frente\n", ano, ano3); encontrar_inicio(f, ano, offset/2); //go forward in file } } } } i've been changing couple of things can't further this. appreciated.
i apologise if there wrong question it's first one.
the file reads (with decades in middle):
year,age,marst,sex,people 1850,0,0,1,1483789 1850,0,0,2,1450376 1850,5,0,1,1411067 2000,90,4,2,29292 2000,90,5,1,147615 2000,90,5,2,774069 2000,90,6,1,15627 2000,90,6,2,59113 edit - ended doing this, , it's working charm.
void encontrar_inicio (file *f, int ano, long offsetmin, long offsetmax) { int ano2; char aux[100], aux2[200], aux3[100], *aux4, lixo[100], lixo2[100]; long meio; meio = (offsetmin+offsetmax)/2; int anoinicio = inicio_ficheiro_ano(f); if(ano==anoinicio) { printf("%d igual %d, começar ler", ano, anoinicio); //inserir dados em; return; } fseek( f, meio, seek_set); fgets(aux, 100, f); fgets(aux2, 200, f); aux4 = strtok(aux2, ","); ano2 = atoi(aux4); if(ano==ano2) { printf("%d é igual %d anos iguais, anda para trás\n", ano, ano2); //return; encontrar_inicio(f, ano, offsetmin, meio);//go in file } if(ano != ano2) { if(ano < ano2) { printf("%d é menor q %d ano menor, anda para trás\n", ano, ano2); //return; encontrar_inicio(f, ano, offsetmin, meio);//go in file } if(ano > ano2) { fgets(lixo, 100, f);//next line fgets(aux3, 5, f); int ano3 = atoi(aux3); if(ano==ano3) { printf("%d é igual %d encontrou o ano, começa ler\n", ano, ano3); fseek(f, -strlen(aux2), seek_cur); return; //começa ler } if(ano!=ano3) { printf("%d é maior que %d ano maior, anda para frente\n", ano, ano3); encontrar_inicio(f, ano, meio, offsetmax); //go forward in file } } } }
you might want try change first encontrar_inicio with:
encontrar_inicio(f, ano, offset/2); (your version using negative offset)
and on second with:
encontrar_inicio(f, ano, offset + ((maxoffset - offset)/2)); (your version going back, instead of going forward. offset greater offset/2)
Comments
Post a Comment