mardi 4 août 2015

An efficient way of reading integers from file

I'd like to read all integers from a file into the one list/array. All numbers are separated by space (one or more) or end line character (one or more). What is the most efficient and/or elegant way of doing this? Functions from c++ library are prohibited.

I did it this way:

/* We assume that 'buffer' is big enaugh */
int i = 0;
while(fscanf(fp, "%d", &buffer[i]) != EOF) {
    ++i;
}

Sample data:

1   2     3
 4 56
    789         
9          91 56   

 10 
11

Aucun commentaire:

Enregistrer un commentaire