Reading long file by parts

Asked

Viewed 30 times

0

I have the following code where I read and print parts of an almost long file 5 million lines.

My problem is that I can’t print more than 64443 lines of this file, which is almost nothing compared to the total size of the file.

I would be satisfied if I could print these 64443 lines in a random way, or sequentially continuing from where to print.

Could someone give a hint?

#include <stdio.h>
#include <conio.h>

int main(){
    char *matriz;
    matriz = ( char * ) malloc ( 1000000 );//1 milhão de bytes ou 1MB
    int i;
    FILE *arq = fopen("Meuarquivo.txt", "r" );//113MB
    if ( arq == NULL ){
        textcolor(YELLOW);gotoxy(26,12);
        printf("Problemas na abertura do arquivo ");
        getche();
        return (0);
    }
    textcolor(WHITE);
    for ( i = 0; i < 64443; i++ ) {//Só lê e imprime até esta linha 64443.
        fgets ( matriz, 255, arq );
        printf("%d %s ", i, matriz );
    }
    //Tentei realocar mais memória más não adianta, a contagem
    //das linhas prossegue, más sai vazio, não imprime mais nada do arquivo
    //tentei resetar a matriz de várias forma, para preencher com dados
    //a partir da linha 64443 más também não resolveu.
    matriz = ( char * ) realloc ( matriz, 2000000  );//2 milhões de bytes ou 2MB
    textcolor(WHITE);
    for ( i = 64443; i < 128000; i++ ) {
        fgets ( matriz, 255, arq );
        printf("%d %s ", i, matriz );
    }
    fclose(arq);
    getche();
}
  • Like evil not equal

  • Someone could edit the code, it’s a mess.rss...

  • In this case of similarity, solving one, the other is solved with a little effort.

  • Thank you very much try this please!!

  • The answer in the other post served me nothing, does not work.

  • Still, it seems to be the same question, so you better wait for another answer over there.

Show 1 more comment
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.