Opening and reading giant files in C

-1

How can I open a file for C language reading, over 100 MB?

With this code placed below, I can read a file with more than 18,000 lines, but what I really need is to read a file with approximately 5,000,000 lines.

Is this possible in C?

char **xxxx;
    if ( ( arq = fopen("Meuarquivo.txt", "r+b" ) ) == NULL ){
        printf("Houve um erro na abertura do arquivo");
        getche();exit(1);
    }
xxxx = ( char** ) malloc ( 19000 *sizeof ( char* ) );
    while ( feof ( arq ) == 0 ){
        nomes [ c ] = ( char* ) malloc ( 19000 *sizeof ( char ) );
        fgets ( nomes [ c ], 19000, arq );
        ++c;
    }
    
asked by anonymous 14.12.2014 / 18:10

1 answer

-1
char **xxxx;
if ((arq = fopen("Meuarquivo.txt", "r+b")) == NULL) {
    printf("Houve um erro na abertura do arquivo");
    getche();
    exit(1);
}
int c = 0;
xxxx = malloc(5000000 * sizeof *xxxx); // 5 milhoes de linhas
if (!xxxx) /* erro */;
while (1) {
    xxxx[c] = malloc(100);             // com no maximo 99 caracteres cada
    if (!xxx[c]) /* erro */;
    if (fgets(xxxx[c], 100, arq) == NULL) /* erro */;
    ++c;
    if (c = 5000000) /* erro */;
}

// usar xxxx

for (int k = 0; k < c; k++) free(xxxx[k]);
free(xxxx);
    
14.12.2014 / 18:29